我正在尝试从主要活动中接收行ID,并从那里显示该行的内容。但是我得到一个空指针异常。我不确定如何正确执行此操作。
该异常与第22行有关MODULEDATABASE.openToRead();。我不确定是否需要打开以阅读,因为我已经在主要活动中这样做了。如果我将这一行注释掉,则下一个错误是指向第23行的游标= 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;
}
最佳答案
我会检查一下myContext是什么。
myHelper = new DbHelper(myContext);
您已将MODULEDATABASE创建为正确的全局变量,但是您需要在Course活动的onCreate方法中初始化对象。确保数据库对象的初始化接受您的“课程”活动中的上下文。也就是说,有一个构造函数,该构造函数具有用于课程活动的参数来传递其上下文。
在您的课程活动中,您应该能够执行以下操作...
database MODULEDATABASE;
public void onCreate(...) {
MODULEDATABASE= new database(getApplicationContext());
}
编辑:以上解决方案在创建对象时提供了正确的上下文。使用ADR的解决方案而不是ViewCourse.this,使用getApplicationContext()应该为您提供了另一种方法。
关于android - Android:从SQlite获取单行,空指针异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13215371/