程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> listview-ListView中OnItemClickListener事件不能執行

listview-ListView中OnItemClickListener事件不能執行

編輯:編程綜合問答
ListView中OnItemClickListener事件不能執行

Activity類:

private String[] mStrings2={"Title 01","Title 02"};
private String[] mStrings = {"Link to the image1", "Link to the image2"};

MyAdapter adapter;

        mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {              

            adapter=new LazyAdapter(this, mStrings, mStrings2);
            subList.setAdapter(adapternew);                       

        }
    });


MyAdapter class (MyAdapter.java)

    private Activity activity;
    private String[] data;
    private String[] data2;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

//Constructor
public LazyAdapter(Activity a, String[] d, String[] mStrings2) {
        activity = a;
        data=d;
        data2=mStrings2;
        inflater = LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

當我點擊第一個 List Item,我想把元素加載到第二個數組中。但是當執行程序後,獲得NullPointerException異常。就是說ListView中OnItemClickListener事件不能執行。大家能指導下哪出錯了嗎?

12-04 14:21:42.499: E/AndroidRuntime(1429): java.lang.NullPointerException
12-04 14:21:42.499: E/AndroidRuntime(1429):     at com.java.hotelapp.Item_List$1.onItemClick(Item_List.java:106)
12-04 14:21:42.499: E/AndroidRuntime(1429):     at android.widget.AdapterView.performItemClick(AdapterView.java:282)
12-04 14:21:42.499: E/AndroidRuntime(1429):     at android.widget.AbsListView.performItemClick(AbsListView.java:1037)
12-04 14:21:42.499: E/AndroidRuntime(1429):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2449)
12-04 14:21:42.499: E/AndroidRuntime(1429):     at android.widget.AbsListView$1.run(AbsListView.java:3073)
12-04 14:21:42.499: E/AndroidRuntime(1429):     at android.os.Handler.handleCallback(Handler.java:587)
12-04 14:21:42.499: E/AndroidRuntime(1429):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-04 14:21:42.499: E/AndroidRuntime(1429):     at android.os.Looper.loop(Looper.java:132)
12-04 14:21:42.499: E/AndroidRuntime(1429):     at android.app.ActivityThread.main(ActivityThread.java:4123)
12-04 14:21:42.499: E/AndroidRuntime(1429):     at java.lang.reflect.Method.invokeNative(Native Method)
12-04 14:21:42.499: E/AndroidRuntime(1429):     at java.lang.reflect.Method.invoke(Method.java:491)
12-04 14:21:42.499: E/AndroidRuntime(1429):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
12-04 14:21:42.499: E/AndroidRuntime(1429):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
12-04 14:21:42.499: E/AndroidRuntime(1429):     at dalvik.system.NativeStart.main(Native Method)

最佳回答:


adapter=new LazyAdapter(this, mStrings, mStrings2);
subList.setAdapter(adapternew);
adapternew是啥,改為adapter試試

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved