我试图创建一个应该生成 ListView 的新类,但是我有一个错误。错误说无法启动活动我很困惑;谁能告诉我问题出在哪里,我做错了什么或者我能做什么?
代码
public class ManagerView extends ListActivity{
DBAdapter db;
Cursor cr;
String arr[];
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = new DBAdapter(this);
cr = db.getAllRecords();
int i = 0;
Cursor crr = null;
try {crr = db.getRecord(4);}
catch (IOException e) {e.printStackTrace();}
for (cr.moveToFirst(); !cr.moveToLast(); cr.moveToNext()){
arr[i] = crr.getString(i);
i++;
}
setListAdapter (new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr));
}
}
错误
12-30 17:11:47.352: E/AndroidRuntime(1299): FATAL EXCEPTION: main
12-30 17:11:47.352: E/AndroidRuntime(1299): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tsfc/com.example.tsfc.ManagerView}: java.lang.NullPointerException
12-30 17:11:47.352: E/AndroidRuntime(1299): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-30 17:11:47.352: E/AndroidRuntime(1299): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-30 17:11:47.352: E/AndroidRuntime(1299): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-30 17:11:47.352: E/AndroidRuntime(1299): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-30 17:11:47.352: E/AndroidRuntime(1299): at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 17:11:47.352: E/AndroidRuntime(1299): at android.os.Looper.loop(Looper.java:137)
12-30 17:11:47.352: E/AndroidRuntime(1299): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-30 17:11:47.352: E/AndroidRuntime(1299): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 17:11:47.352: E/AndroidRuntime(1299): at java.lang.reflect.Method.invoke(Method.java:511)
12-30 17:11:47.352: E/AndroidRuntime(1299): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-30 17:11:47.352: E/AndroidRuntime(1299): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-30 17:11:47.352: E/AndroidRuntime(1299): at dalvik.system.NativeStart.main(Native Method)
12-30 17:11:47.352: E/AndroidRuntime(1299): Caused by: java.lang.NullPointerException
12-30 17:11:47.352: E/AndroidRuntime(1299): at com.example.tsfc.DBAdapter.getAllRecords(DBAdapter.java:100)
12-30 17:11:47.352: E/AndroidRuntime(1299): at com.example.tsfc.ManagerView.onCreate(ManagerView.java:32)
12-30 17:11:47.352: E/AndroidRuntime(1299): at android.app.Activity.performCreate(Activity.java:5104)
12-30 17:11:47.352: E/AndroidRuntime(1299): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-30 17:11:47.352: E/AndroidRuntime(1299): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-30 17:11:47.352: E/AndroidRuntime(1299): ... 11 more