选择布局 - 错误

时间:2012-02-27 19:57:31

标签: android

我犯了哪个错误?

public class WyborActivity extends Activity {

private SharedPreferences mSharedPreferences;
public static final String MY_PREFERENCES = "myPreferences";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final RadioButton theme0 = (RadioButton) findViewById(R.id.theme0);
    final RadioButton theme1 = (RadioButton) findViewById(R.id.theme1);
    theme0.setChecked(mSharedPreferences.getString("icon", "one")
            .equals("b_2"));

    theme0.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            if (!isChecked)
                return;
            SharedPreferences.Editor editor = mSharedPreferences.edit();

            editor.putString("icon", "one");
            theme1.setChecked(false);
            //resetService();

            editor.commit();
        }
    });

    theme1.setChecked(mSharedPreferences.getString("icon", "two")
            .equals("b_2"));

    theme1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            if (!isChecked)
                return;
            SharedPreferences.Editor editor = mSharedPreferences.edit();

            editor.putString("icon", "two");
            theme0.setChecked(false);
            //resetService();

            editor.commit();
        }
    });

}
//protected void resetService() {
//  // TODO Auto-generated method stub
//  resetService();
//}

}

02-27 19:40:40.395: E/AndroidRuntime(745): Uncaught handler: thread main exiting due to uncaught exception
02-27 19:40:40.425: E/AndroidRuntime(745): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wybor/com.wybor.WyborActivity}: java.lang.NullPointerException
02-27 19:40:40.425: E/AndroidRuntime(745):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
02-27 19:40:40.425: E/AndroidRuntime(745):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
02-27 19:40:40.425: E/AndroidRuntime(745):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
02-27 19:40:40.425: E/AndroidRuntime(745):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
02-27 19:40:40.425: E/AndroidRuntime(745):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 19:40:40.425: E/AndroidRuntime(745):  at android.os.Looper.loop(Looper.java:123)
02-27 19:40:40.425: E/AndroidRuntime(745):  at android.app.ActivityThread.main(ActivityThread.java:4363)
02-27 19:40:40.425: E/AndroidRuntime(745):  at java.lang.reflect.Method.invokeNative(Native Method)
02-27 19:40:40.425: E/AndroidRuntime(745):  at java.lang.reflect.Method.invoke(Method.java:521)
02-27 19:40:40.425: E/AndroidRuntime(745):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-27 19:40:40.425: E/AndroidRuntime(745):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-27 19:40:40.425: E/AndroidRuntime(745):  at dalvik.system.NativeStart.main(Native Method)
02-27 19:40:40.425: E/AndroidRuntime(745): Caused by: java.lang.NullPointerException
02-27 19:40:40.425: E/AndroidRuntime(745):  at com.wybor.WyborActivity.onCreate(WyborActivity.java:24)
02-27 19:40:40.425: E/AndroidRuntime(745):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-27 19:40:40.425: E/AndroidRuntime(745):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
02-27 19:40:40.425: E/AndroidRuntime(745):  ... 11 more
02-27 19:40:40.465: I/dalvikvm(745): threadid=7: reacting to signal 3
02-27 19:40:40.465: E/dalvikvm(745): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

1 个答案:

答案 0 :(得分:1)

您需要先初始化mSharedPreferences变量,然后才能从中获取信息。

初始化它应该看起来像这样:

// intialize Shared Preferences
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
// then get information from it
theme0.setChecked(mSharedPreferences.getString("icon", "one").equals("b_2"));
相关问题