0

我想做一個自定義列表,它是從數據庫中獲取數據,並有兩個文本視圖,但它給NPE,我不明白爲什麼是這樣,並砸太多head.Please幫助 這裏是我的代碼:NPE在自定義列表

public class NotesList extends ListActivity{ 
    private DataSource datasource; 
    ArrayAdapter<Message> adapter; 
    CustomList adap; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.list); 

     // List<Message> values = datasource.getAllMessages(); 
    // adapter = new ArrayAdapter<Message>(this, android.R.layout.simple_list_item_1, values); 
    // setListAdapter(adapter); 
     adap = new CustomList(); 

     setListAdapter(adap); 



    } 
    @Override 
    public void onResume() { 
     datasource.open(); 

     super.onResume(); 
     } 


     @Override 
    public void onPause() { 
     datasource.close(); 

     super.onPause(); 

     } 
     public class CustomList extends BaseAdapter { 

      List<Message> DataList = datasource.getAllMessages(); 
      @Override 
      public int getCount() { 
       // TODO Auto-generated method stub 
       return DataList.size(); 
      } 

      @Override 
      public Message getItem(int arg0) { 
       // TODO Auto-generated method stub 
       return DataList.get(arg0); 
      } 

      @Override 
      public long getItemId(int arg0) { 
       // TODO Auto-generated method stub 
       return arg0; 
      } 

      @Override 
      public View getView(int arg0, View arg1, ViewGroup arg2) { 

       if(arg1==null) 
       { 
        LayoutInflater inflater = (LayoutInflater) NotesList.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        arg1 = inflater.inflate(R.layout.listitem, arg2,false); 
       } 

       TextView heading = (TextView)arg1.findViewById(R.id.textView1); 
       TextView notes = (TextView)arg1.findViewById(R.id.textView2); 

       Message MessageObj = DataList.get(arg0); 

       heading.setText(MessageObj.getmessage()); 
       notes.setText(MessageObj.getmessage()); 

       return arg1; 
      } 

      public Message getCodeLearnChapter(int position) 
      { 
       return DataList.get(position); 
      } 

     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
     // getMenuInflater().inflate(R.menu.list_view_with_simple_adapter, menu); 
      return true; 
     } 

} 

logcat的是:

03-19 07:24:00.936: E/AndroidRuntime(28816): FATAL EXCEPTION: main 
03-19 07:24:00.936: E/AndroidRuntime(28816): java.lang.RuntimeException: Unable to start activity ComponentInfo{soft.b.notes/soft.b.notes.NotesList}: java.lang.NullPointerException 
03-19 07:24:00.936: E/AndroidRuntime(28816): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955) 
03-19 07:24:00.936: E/AndroidRuntime(28816): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980) 
03-19 07:24:00.936: E/AndroidRuntime(28816): at android.app.ActivityThread.access$600(ActivityThread.java:122) 
03-19 07:24:00.936: E/AndroidRuntime(28816): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146) 
03-19 07:24:00.936: E/AndroidRuntime(28816): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-19 07:24:00.936: E/AndroidRuntime(28816): at android.os.Looper.loop(Looper.java:137) 
03-19 07:24:00.936: E/AndroidRuntime(28816): at android.app.ActivityThread.main(ActivityThread.java:4340) 
03-19 07:24:00.936: E/AndroidRuntime(28816): at java.lang.reflect.Method.invokeNative(Native Method) 
03-19 07:24:00.936: E/AndroidRuntime(28816): at java.lang.reflect.Method.invoke(Method.java:511) 
03-19 07:24:00.936: E/AndroidRuntime(28816): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
03-19 07:24:00.936: E/AndroidRuntime(28816): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 

商祺!

+1

'datasource'初始化在哪裏? – Raghunandan

+1

發佈堆棧跟蹤的其餘部分。 – njzk2

回答

2

你永遠不會初始化datasource和調用datasource.getAllMessages()原因NPE

1

NPE能夠在下面的logcat中發現,有幫助的堆棧跟蹤診斷它。解釋NPE

一種方式是

List<Message> DataList = datasource.getAllMessages(); 

調用,當你調用new CustomList()未初始化的datasource的方法。

+0

抱歉:(................. – user3091531