我正在尝试创建一个自定义列表,它从数据库中获取数据,并且有两个文本视图,但它给出了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)
问候!
答案 0 :(得分:2)
您永远不会初始化datasource
并调用datasource.getAllMessages()
导致NPE
答案 1 :(得分:1)
可以在logcat中找到NPE,并使用有用的堆栈跟踪来诊断它。
解释NPE的一种方法是
List<Message> DataList = datasource.getAllMessages();
调用datasource
上的方法,当您拨打new CustomList()
时,该方法未初始化。