1

我试图创建一个应该生成 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    
4

2 回答 2

1

找不到显式活动类 {com.example.tsfc/com.example.tsfc.ManagerView};您是否在 AndroidManifest.xml 中声明了此活动?12-30 16:35:09.451: E/AndroidRuntime(1049): 在

确保您已将您的活动声明AndroidManifest为:

<activity android:name="com.example.tsfc.ManagerView" />

并且目前您正在错误的地方初始化 DBAdapter 。将 ListActivity 的 onCreate 中的所有数据库类初始化和 getAllRecords 移动为:

public  class ManagerView extends ListActivity{

DBAdapter db ;
Cursor cr ;
String arr[];



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


     db = new DBAdapter(ManagerView.this);
     cr = db.getAllRecords();
}

// your code here,...

}
于 2012-12-30T14:55:11.740 回答
-1

你有一个空指针错误。删除错误。

或者

在 onCreate() 函数中注释除 setContentView() 之外的所有代码,并查看活动是否成功打开。

于 2012-12-30T16:14:29.097 回答