我试图显示最近十个数据库对象的ListView。如果我在标签中显示这些对象,一切都正常,但现在我创建了ListView+DataAdapter,但应用程序崩溃,出现NullPointerException。我搜索了很多,但我找到的唯一解决方案是ArrayList没有用=new ArrayList()初始化,或者视图的名称与findViewById方法中的名称不匹配,但在我的项目中没有一个是错误的。感谢您的帮助:)
主要活动.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //res/layout/activity_main.xml Layout
list = (ListView) findViewById(R.id.lastpushlist);
list.setAdapter(new DataAdapter(this));
数据适配器.java
Controller aController = new Controller();
Context context;
ArrayList<SingleRow> list = aController.getLastLogs(MainActivity.regIDglobal, "10");
DataAdapter(Context c){
context = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, parent,false);
TextView Message = (TextView) row.findViewById(R.id.textView1);
TextView URL = (TextView) row.findViewById(R.id.textView2);
SingleRow temp = list.get(position);
Message.setText(temp.message);
URL.setText(temp.url);
return row;
}
控制器.java
ArrayList<SingleRow> list = new ArrayList<SingleRow>();
// Lots of Database Calls and get JSON Objects but for Testing i just create and add some Test Data into the list and return it.
list.add(new SingleRow("bla","bla2"));
return list;
活动_最小.xml
<ListView
android:id="@+id/lastpushlist"
android:layout_height="fill_parent"
android:layout_width="fill_parent" >
</ListView>
日志目录:
02-24 12:20:23.468: D/AndroidRuntime(1475): Shutting down VM
02-24 12:20:23.472: D/dalvikvm(1475): GC_CONCURRENT freed 440K, 19% free 2720K/3336K, paused 4ms+0ms, total 16ms
02-24 12:20:23.476: W/dalvikvm(1475): threadid=1: thread exiting with uncaught exception (group=0xa615d908)
02-24 12:20:23.476: E/AndroidRuntime(1475): FATAL EXCEPTION: main
02-24 12:20:23.476: E/AndroidRuntime(1475): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.fhdortmund.pushprojekt/de.fhdortmund.pushprojekt.MainActivity}: java.lang.NullPointerException
02-24 12:20:23.476: E/AndroidRuntime(1475): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-24 12:20:23.476: E/AndroidRuntime(1475): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-24 12:20:23.476: E/AndroidRuntime(1475): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-24 12:20:23.476: E/AndroidRuntime(1475): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-24 12:20:23.476: E/AndroidRuntime(1475): at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 12:20:23.476: E/AndroidRuntime(1475): at android.os.Looper.loop(Looper.java:137)
02-24 12:20:23.476: E/AndroidRuntime(1475): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-24 12:20:23.476: E/AndroidRuntime(1475): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 12:20:23.476: E/AndroidRuntime(1475): at java.lang.reflect.Method.invoke(Method.java:511)
02-24 12:20:23.476: E/AndroidRuntime(1475): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-24 12:20:23.476: E/AndroidRuntime(1475): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-24 12:20:23.476: E/AndroidRuntime(1475): at dalvik.system.NativeStart.main(Native Method)
02-24 12:20:23.476: E/AndroidRuntime(1475): Caused by: java.lang.NullPointerException
02-24 12:20:23.476: E/AndroidRuntime(1475): at de.fhdortmund.pushprojekt.DataAdapter.<init>(DataAdapter.java:16)
02-24 12:20:23.476: E/AndroidRuntime(1475): at de.fhdortmund.pushprojekt.MainActivity.onCreate(MainActivity.java:49)
02-24 12:20:23.476: E/AndroidRuntime(1475): at android.app.Activity.performCreate(Activity.java:5104)
02-24 12:20:23.476: E/AndroidRuntime(1475): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-24 12:20:23.476: E/AndroidRuntime(1475): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-24 12:20:23.476: E/AndroidRuntime(1475): ... 11 more
DataAdapter第16行为:ArrayList list=aController.getLastLogs(MainActivity.regIDglobal,“10”);
主活动行49是:list.setAdapter(新的DataAdapter(this));