2 baby bonnie Baby_Bonnie 于 2012.12.05 11:15 提问

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)

3个回答

franzhong
franzhong   2012.12.05 13:44
已采纳

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

A328240784
A328240784   2012.12.05 12:40

adapter=new LazyAdapter(this, mStrings, mStrings2);

传this不会报编译时错误???

A328240784
A328240784 你得明确知道你的空指针异常是哪个对象为Null
大约 5 年之前 回复
Baby_Bonnie
Baby_Bonnie 没有报错,就是点击First list item时,会报错NullPointerException。
大约 5 年之前 回复
cytown
cytown   2012.12.05 14:30

subList是null,检查在哪里初始化的。
subList是null,检查在哪里初始化的。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!