“无法开始活动”,LOGCAT说

时间:2012-12-30 14:49:59

标签: android listview

我试图创建一个应该生成ListView的新类,但是我有一个错误.. 错误说无法开始活动我很困惑;任何人都可以告诉我问题在哪里,我做错了什么或我能做什么?

CODE

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));
    }

}

ERROR

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    

2 个答案:

答案 0 :(得分:1)

  

无法找到显式活动类   {com.example.tsfc / com.example.tsfc.ManagerView};你宣布了​​吗?   AndroidManifest.xml中的这个活动? 12-30 16:35:09.451:   E / AndroidRuntime(1049):at

确保您已将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,...

}

答案 1 :(得分:-1)

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

在onCreate()函数中注释所有代码但setContentView(),看看活动是否成功打开。