我正在尝试从主要活动接收行 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;
}