質問

I'm trying to make it so when I click a button it adds what is in my edit text to my SQLite table but when I click it, it just crashes. I'm getting an onpause error. Here is the log cat.

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

Here's my onpause method

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

And my saveState method

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

I'm really new to databases and what not, so I'm sorry if this is an easy error. But if you could help me that'd be great. Thank you so much. If you need any other code just tell me I will post immediately!

役に立ちましたか?

解決

The null pointer is due to the fact that mCategory is null. Based on the code you posted, your view does not contain a Spinner with the id "category". That explains why getViewById(R.id.category) returned null.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top