1

我正在尝试从主要活动接收行 ID,并从那里显示该行的内容。但是我得到一个空指针异常。我不确定如何正确实施。

该异常与第 22 行MODULEDATABASE.openToRead();相关。我不确定是否需要打开阅读,因为我在主要活动中已经这样做了。如果我注释掉这一行,下一个错误是指第 23 行 cursor = MODULEDATABASE.getRow(rowId); 我们欢迎所有的建议

来自 **MODULEDATABASE.openToRead() 的 LogCat 错误;

    10-23 09:12:52.572: E/AndroidRuntime(19778): Uncaught handler: thread main exiting due to uncaught exception
10-23 09:12:52.582: E/AndroidRuntime(19778): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mycoursetimetable/com.example.mycoursetimetable.ViewCourse}: java.lang.NullPointerException
10-23 09:12:52.582: E/AndroidRuntime(19778):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at android.app.ActivityThread.access$2200(ActivityThread.java:119)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at android.os.Looper.loop(Looper.java:123)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at android.app.ActivityThread.main(ActivityThread.java:4363)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at java.lang.reflect.Method.invokeNative(Native Method)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at java.lang.reflect.Method.invoke(Method.java:521)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at dalvik.system.NativeStart.main(Native Method)
10-23 09:12:52.582: E/AndroidRuntime(19778): Caused by: java.lang.NullPointerException
10-23 09:12:52.582: E/AndroidRuntime(19778):    at com.example.mycoursetimetable.ViewCourse.onCreate(ViewCourse.java:22)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
10-23 09:12:52.582: E/AndroidRuntime(19778):    ... 11 more
10-23 09:12:52.603: I/dalvikvm(19778): threadid=7: reacting to signal 3
10-23 09:12:52.603: E/dalvikvm(19778): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
10-23 09:12:59.232: I/Process(19778): Sending signal. PID: 19778 SIG: 9

LogCat 游标 = MODULEDATABASE.getRow(rowId);

10-23 09:16:42.923: E/AndroidRuntime(20316): Uncaught handler: thread main exiting due to uncaught exception
10-23 09:16:42.933: E/AndroidRuntime(20316): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mycoursetimetable/com.example.mycoursetimetable.ViewCourse}: java.lang.NullPointerException
10-23 09:16:42.933: E/AndroidRuntime(20316):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at android.app.ActivityThread.access$2200(ActivityThread.java:119)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at android.os.Looper.loop(Looper.java:123)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at android.app.ActivityThread.main(ActivityThread.java:4363)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at java.lang.reflect.Method.invokeNative(Native Method)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at java.lang.reflect.Method.invoke(Method.java:521)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at dalvik.system.NativeStart.main(Native Method)
10-23 09:16:42.933: E/AndroidRuntime(20316): Caused by: java.lang.NullPointerException
10-23 09:16:42.933: E/AndroidRuntime(20316):    at com.example.mycoursetimetable.ViewCourse.onCreate(ViewCourse.java:23)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
10-23 09:16:42.933: E/AndroidRuntime(20316):    ... 11 more
10-23 09:16:42.953: I/dalvikvm(20316): threadid=7: reacting to signal 3
10-23 09:16:42.953: E/dalvikvm(20316): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
10-23 09:16:44.633: I/Process(20316): Sending signal. PID: 20316 SIG: 9

查看课程活动

public class ViewCourse extends Activity {
Cursor cursor;
database MODULEDATABASE;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_course);

        Intent intent = getIntent();
        String rowId = intent.getStringExtra(MyCourses.TEST);
        //MODULEDATABASE.openToRead();
        cursor = MODULEDATABASE.getRow(rowId);

        TextView text_modulecode = (TextView)findViewById(R.id.viewModuleCode);
        TextView text_modulename = (TextView)findViewById(R.id.viewModuleName);

        text_modulecode.setText(cursor.getString(cursor.getColumnIndex(database.KEY_MODULECODE)));
        text_modulename.setText(cursor.getString(cursor.getColumnIndex(database.KEY_MODULENAME)));

    }

数据库类

public  database openToRead()throws SQLException{
        myHelper = new DbHelper(myContext);
        moduleDatabase = myHelper.getReadableDatabase();
        return this;
}

public Cursor getRow(String rowId)
    {
        String[] columns = new String [] {KEY_ROWID,KEY_MODULECODE, KEY_MODULENAME};
        Cursor c = moduleDatabase.query(DATABASE_TABLE, columns,KEY_ROWID+ "="+rowId , null, null, null, null);
        if(c!=null){
            c.moveToFirst();
        }
        return c;
    }
4

1 回答 1

1

我会检查 myContext 是什么。

myHelper = new DbHelper(myContext);

您已将 MODULEDATABASE 创建为全局的,这是正确的,但是您需要在 Course 活动的 onCreate 方法中初始化该对象。确保数据库对象的初始化接受来自 Course 活动的上下文。也就是说,有一个构造函数,该构造函数具有用于传递其上下文的课程活动的参数。

在您的课程活动中,您应该能够执行以下操作...

database MODULEDATABASE;
public void onCreate(...) {
    MODULEDATABASE= new database(getApplicationContext()); 
} 

编辑:上面的解决方案在创建对象时提供了正确的上下文。使用 ADR 的解决方案而不是 ViewCourse.this,使用 getApplicationContext() 应该为您提供另一种执行此操作的方法。

于 2012-11-04T02:30:38.877 回答