android中没有启动活动的空指针异常

时间:2015-03-20 16:56:14

标签: android sharedpreferences

这是我的代码 MainActivity 。无法找到我出错的地方任何帮助都会受到赞赏,因为我是Android的新手。在共享首选项中保存值但是当我运行它时会抛出异常,如下面的logcat中所述

public class MainActivity extends Activity implements View.OnClickListener {

CheckBox checkBox;
EditText editText;
Button button;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    checkBox = (CheckBox) findViewById(R.id.checkbox);
    editText = (EditText) findViewById(R.id.editText);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(this);
    loadSavedPreferences();
}





private void loadSavedPreferences() {
    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(this);
    boolean checkBoxValue = sharedPreferences.getBoolean("CheckBox_Value", false);
    String name = sharedPreferences.getString("storedName", "YourName");
    if (checkBoxValue) {
        checkBox.setChecked(true);
    } else {
        checkBox.setChecked(false);
    }
    editText.setText(name);
}

private void savePreferences(String key, boolean value) {

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(key, value);
    editor.commit();
}

private void savePreferences(String key, String value) {

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
}

@Override
public void onClick(View v) {
    savePreferences("CheckBox_Value", checkBox.isChecked());
    if (checkBox.isChecked())
    savePreferences("storedName", editText.getText().toString());
    finish();

}

}

这是logcat

03-20 21:38:28.420    4574-4574/com.example22.dell.sharedprefences E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example22.dell.sharedprefences, PID: 4574
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example22.dell.sharedprefences/com.example22.dell.sharedprefences.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5292)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example22.dell.sharedprefences.MainActivity.onCreate(Unknown Source)
            at android.app.Activity.performCreate(Activity.java:5264)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5292)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
            at dalvik.system.NativeStart.main(Native Method)

布局xml文件

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_below="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_marginTop="89dp"
    android:layout_below="@+id/editText"
    android:layout_centerHorizontal="true" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Are You Sure ?"
    android:id="@+id/checkBox"
    android:layout_below="@+id/editText"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:checked="false" />

1 个答案:

答案 0 :(得分:2)

xml中复选框的ID是&#34;检查 B ox&#34;在您的代码中使用&#34;检查 b ox&#34;。

我不知道您的构建工具,但在R.java文件中,不应存在常量R.id.checkbox,除非您重构它并且R.java没有更新。