2012-06-12 153 views
0

我Activity類的Android的ListView OnItemClickListener事件不工作

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()); 
    } 

當我點擊第一個列表項,我要加載第二個數組元素。但是當執行代碼時,我得到「NullPointerException」。如何解決這個問題。請指導我。

編輯....

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

顯示NPE發生的logcat。 – ariefbayu

回答

0

我看不到adapternew適配器的聲明,以便它可以null ..

subList.setAdapter(adapternew);   

該線路應該是,

adapter = new LazyAdapter(MyActivity.this, mStrings, mStrings2); 
subList.setAdapter(adapter); 
+0

是的,我糾正了它,但仍然當我點擊第一個列表項目獲取NullPointerException – Senps

+0

現在,張貼您的應用程序logcat報告.. – user370305

+0

還提供您的活動類的完整代碼... – user370305

0

也許另一個建議:

mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() 

難道是mainList == null

而且,請提供正確的代碼和確切的故障時刻和線路。堆棧跟蹤的特定部分也很好。

0
ListView lv = (ListView)this.findViewById(R.id.listviewid); 
lv.setItemsCanFocus(false); 

or try onlistitemclick 

@Override  
protected void onListItemClick(ListView lv, View v, int position, long id) {  
    // TODO Auto-generated method stub 
    super.onListItemClick(lv, v, position, id); 
} 
相關問題