2012-02-27 36 views
0

我在哪裏犯了一個錯誤?選擇佈局 - 錯誤

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 
+0

你的錯誤日誌顯示在NPE線24時,心中強調此行我們呢? – 2012-02-27 20:01:35

回答

1

需要初始化你mSharedPreferences變量,然後才能從中獲取信息。

初始化它看起來應該有點像這樣:

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

謝謝,我的不好。 :) – Defuzer 2012-02-27 20:22:26

+0

沒問題,如果這解決了您的問題,請使用複選標記將其標記爲「已接受的答案」。乾杯。 – 2012-02-27 20:39:58