尝试将项目添加到SQLite数据库时应用程序崩溃(onPause()错误)

时间:2013-11-21 02:14:05

标签: android database sqlite android-activity onpause

我正在尝试这样做,当我点击一个按钮时,它会将我的编辑文本中的内容添加到我的SQLite表中,但是当我单击它时,它就会崩溃。我收到了暂停错误。这是日志猫。

11-20 20:06:32.076: E/AndroidRuntime(10548): FATAL EXCEPTION: main
11-20 20:06:32.076: E/AndroidRuntime(10548): java.lang.RuntimeException: Unable to pause activity {com.oxpheen.tweetschedulerfree/com.oxpheen.tweetschedulerfree.ScheduleTweetDialog}: java.lang.NullPointerException
11-20 20:06:32.076: E/AndroidRuntime(10548):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3144)
11-20 20:06:32.076: E/AndroidRuntime(10548):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3099)
11-20 20:06:32.076: E/AndroidRuntime(10548):    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3077)
11-20 20:06:32.076: E/AndroidRuntime(10548):    at android.app.ActivityThread.access$800(ActivityThread.java:153)
11-20 20:06:32.076: E/AndroidRuntime(10548):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1264)
11-20 20:06:32.076: E/AndroidRuntime(10548):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 20:06:32.076: E/AndroidRuntime(10548):    at android.os.Looper.loop(Looper.java:137)
11-20 20:06:32.076: E/AndroidRuntime(10548):    at android.app.ActivityThread.main(ActivityThread.java:5227)
11-20 20:06:32.076: E/AndroidRuntime(10548):    at java.lang.reflect.Method.invokeNative(Native Method)
11-20 20:06:32.076: E/AndroidRuntime(10548):    at java.lang.reflect.Method.invoke(Method.java:511)
11-20 20:06:32.076: E/AndroidRuntime(10548):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-20 20:06:32.076: E/AndroidRuntime(10548):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-20 20:06:32.076: E/AndroidRuntime(10548):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
11-20 20:06:32.076: E/AndroidRuntime(10548):    at dalvik.system.NativeStart.main(Native Method)
11-20 20:06:32.076: E/AndroidRuntime(10548): Caused by: java.lang.NullPointerException
11-20 20:06:32.076: E/AndroidRuntime(10548):    at com.oxpheen.tweetschedulerfree.ScheduleTweetDialog.saveState(ScheduleTweetDialog.java:266)
11-20 20:06:32.076: E/AndroidRuntime(10548):    at com.oxpheen.tweetschedulerfree.ScheduleTweetDialog.onPause(ScheduleTweetDialog.java:262)
11-20 20:06:32.076: E/AndroidRuntime(10548):    at android.app.Activity.performPause(Activity.java:5206)
11-20 20:06:32.076: E/AndroidRuntime(10548):    at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1226)
11-20 20:06:32.076: E/AndroidRuntime(10548):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3130)
11-20 20:06:32.076: E/AndroidRuntime(10548):    ... 13 more

这是我的暂停方法

  @Override
  protected void onPause() {
    super.onPause();
    saveState(); //Line 262 where it crashes
  }

我的saveState方法

 private void saveState() {
    String category = (String) mCategory.getSelectedItem(); //Line 266 where it crashes
    String summary = mTitleText.getText().toString();
    String description = mBodyText.getText().toString();

    if (description.length() == 0 && summary.length() == 0) {
      return;
    }

    ContentValues values = new ContentValues();
    values.put (TodoTable.COLUMN_CATEGORY, category == null ? "" : category);
    values.put(TodoTable.COLUMN_SUMMARY, summary);
    values.put(TodoTable.COLUMN_DESCRIPTION, description);

    if (todoUri == null) {
      // New todo
      todoUri = getContentResolver().insert(MyTodoContentProvider.CONTENT_URI, values);
    } else {
      // Update todo
      getContentResolver().update(todoUri, values, null, null);
    }
  }

我对数据库真的很陌生,什么不是,所以如果这是一个简单的错误,我很抱歉。但如果你能帮助我,那就太好了。非常感谢。如果您需要任何其他代码,请告诉我,我会立即发布!

1 个答案:

答案 0 :(得分:1)

空指针是由于mCategory为null。根据您发布的代码,您的视图不包含ID为"类别"的微调器。这就解释了为什么getViewById(R.id.category)返回null。