Java BaseAdapter - ArrayList 作为 Return 值 - NullpointerException
Java BaseAdapter - ArrayList as Return Value - NullpointerException
我正在尝试显示最近十个数据库对象的列表视图。如果我在标签中显示这些对象,一切正常,但现在我创建了一个 ListView + DataAdapter,但应用程序因 NullPointerException 而崩溃。我在谷歌上搜索了很多,但我找到的唯一解决方案是 ArrayList 未使用 = new ArrayList() 初始化,或者视图的名称与 findViewById 方法中的名称不匹配,但其中 none 在我的方法中是错误的项目。感谢您的帮助:)
MainActivity.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));
DataAdapter.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;
}
Controller.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;
activity_main.xml
<ListView
android:id="@+id/lastpushlist"
android:layout_height="fill_parent"
android:layout_width="fill_parent" >
</ListView>
Logcat:
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.access0(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");
MainActivity 第 49 行是:list.setAdapter(new DataAdapter(this));
唯一可能在第 16 行抛出 NPE 的是 aController 是否为 null。您没有 post 创建此对象的代码,所以我认为它根本不是。
我正在尝试显示最近十个数据库对象的列表视图。如果我在标签中显示这些对象,一切正常,但现在我创建了一个 ListView + DataAdapter,但应用程序因 NullPointerException 而崩溃。我在谷歌上搜索了很多,但我找到的唯一解决方案是 ArrayList 未使用 = new ArrayList() 初始化,或者视图的名称与 findViewById 方法中的名称不匹配,但其中 none 在我的方法中是错误的项目。感谢您的帮助:)
MainActivity.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));
DataAdapter.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;
}
Controller.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;
activity_main.xml
<ListView
android:id="@+id/lastpushlist"
android:layout_height="fill_parent"
android:layout_width="fill_parent" >
</ListView>
Logcat:
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.access0(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"); MainActivity 第 49 行是:list.setAdapter(new DataAdapter(this));
唯一可能在第 16 行抛出 NPE 的是 aController 是否为 null。您没有 post 创建此对象的代码,所以我认为它根本不是。