2011-10-29 90 views
1

嗨,我想保存一些複選框的值,我有一個.xml格式的值。 我用這個SharedPreferences上我的主要XML但我得到的消息我不能編譯:保存複選框的值

Cannot invoke isChecked() on the primitive type int 

有人可以幫我嗎? 代碼應該如何?

編輯的代碼:

@Override 
public void onPause() { 
    super.onPause(); 
    final CheckBox checkBox = (CheckBox) findViewById(R.id.nieuwbel); 
    save(checkBox.isChecked()); 
} 

private CheckBox checkBox = null; 

@Override 
public void onResume() { 
    super.onResume(); 
    checkBox = (CheckBox) findViewById(R.id.nieuwbel); 
    checkBox.setChecked(load()); <----- still exception line 464 
} 
private void save(final boolean isChecked) { 
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedPreferences.edit(); 
    editor.putBoolean("check", isChecked); 
    editor.commit(); 
} 

private boolean load() { 
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE); 
    return sharedPreferences.getBoolean("check", false); 
} 

異常代碼的logcat:

10-29 23:35:31.556: E/AndroidRuntime(3292): FATAL EXCEPTION: main 
10-29 23:35:31.556: E/AndroidRuntime(3292): java.lang.RuntimeException: Unable to resume activity {com.sencide/com.sencide.AndroidLogin}: java.lang.NullPointerException 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2241) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2256) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1789) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.app.ActivityThread.access$1500(ActivityThread.java:123) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.os.Handler.dispatchMessage(Handler.java:99) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.os.Looper.loop(Looper.java:130) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.app.ActivityThread.main(ActivityThread.java:3835) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at java.lang.reflect.Method.invoke(Method.java:507) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at dalvik.system.NativeStart.main(Native Method) 
10-29 23:35:31.556: E/AndroidRuntime(3292): Caused by: java.lang.NullPointerException 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at com.sencide.AndroidLogin.onResume(AndroidLogin.java:463) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.app.Activity.performResume(Activity.java:3832) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2231) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  ... 12 more 

編輯2:幾乎與其他用戶一起解決了這個問題,但是當我啓動應用程序並關閉它沒有做任何事情(秀菜單等)我會在onPause上的這條線上得到一個裸眼視線:

putBoolean(PREF_BOOL, nieuwbel.isChecked()); 

The code so遠:

public static final String PREFS_NAME = "SharedPrefsDemoPreferences"; 
public static final String PREF_BOOL = "PrefBool"; 

private SharedPreferences mPrefs; 
private CheckBox nieuwbel; 

public boolean myBoxState = false; 
.. 
mPrefs = getSharedPreferences(PREFS_NAME, 0); 
    nieuwbel = (CheckBox)findViewById(R.id.nieuwbel); 

通過菜單調用佈局的xml:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle item selection 
    switch (item.getItemId()) { 
    case R.id.menu_beltegoed: // Laat Beltegoed.xml zien en checkboxen worden geinitialiseerd 
     MyBeltegoed dialog = new MyBeltegoed (this, new OnReadyListenerBeltegoed()); 
     dialog.setTitle("Optie beltegoed/toon:"); 
     dialog.show(); 
     nieuwbel = (CheckBox)dialog.findViewById(R.id.nieuwbel); 
     nieuwbel.setChecked(myBoxState); 
     nieuwbel.setOnCheckedChangeListener(new OnCheckedChangeListener(){ 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, 
        boolean isChecked) { 
       myBoxState = nieuwbel.isChecked(); 

      }}); 
     return true; 
    case R.id.menu_sluit: 
     //showHelp(); 
     return true; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 
} 

的的onResume和的onSave:

@Override 
protected void onResume() { 
    mPrefs = getSharedPreferences(PREFS_NAME, 0); 
    if(mPrefs!=null) 
     myBoxState=mPrefs.getBoolean(PREF_BOOL, false); 
    super.onResume(); 
} 


@Override  
protected void onPause() { 
    Editor e = mPrefs.edit(); 
    e.putBoolean(PREF_BOOL, nieuwbel.isChecked()); //<-- NullPointer when i start and close the app, without doing some handlings.    
    e.commit();   
    Toast.makeText(this, "Settings Saved.", Toast.LENGTH_SHORT).show();   
    super.onPause();  
} 

編輯3:

@Override  
protected void onPause() { 
    MyBeltegoed dialog = new MyBeltegoed (this, new OnReadyListenerBeltegoed()); 
    nieuwbel = (CheckBox)dialog.findViewById(R.id.nieuwbel); 
    Editor e = mPrefs.edit(); 
    e.putBoolean(PREF_BOOL, nieuwbel.isChecked()); <-- nullpointer    
    e.commit();   
    Toast.makeText(this, "Settings Saved.", Toast.LENGTH_SHORT).show();   
    super.onPause();  
} 
+0

我信步有兩大聲明的複選框,其中之一是最後!爲什麼需要在OnPause中再次聲明覆選框? –

+0

嗨瓦利德, 我發佈了我的佈局xml文件與複選框,你有解釋爲什麼它不工作? – Lars

+0

NullPointerExceptions是最容易找出的例外。這意味着一些東西是空的(這可能意味着它的聲明,但沒有實例化)。很可能nieubwel是空的。請閱讀一些開始的Android教程(http://developer.android.com/resources/tutorials/notepad/index.html)。 – Jack

回答

2

您無法訪問通過使用R.id.nieuwbel.is複選框的值()檢查。以下是方式:

final CheckBox checkBox = (CheckBox) findViewById(R.id.nieuwbel); 
     if (checkBox.isChecked()) { 
      checkBox.setChecked(false); 
     } 

請參閱this鏈接。

所以在onPause應該是這樣的:

@Override 
public void onPause() { 
    super.onPause(); 
    final CheckBox checkBox = (CheckBox) findViewById(R.id.nieuwbel); 
    save(checkBox.isChecked()); 
} 

和的onResume應該是這樣的:

@Override 
public void onResume() { 
    super.onResume(); 
    final CheckBox checkBox = (CheckBox) findViewById(R.id.nieuwbel); 
    checkBox.setChecked(load()); 
} 
+0

嗨傑克, Thx回答,如果我應用您的更正,我會得到forceclose。 看到我編輯的例外代碼。 – Lars

+0

請從logcat發佈異常日誌。 – Jack

+0

Hi Jack, Logcat發佈 – Lars

1

R.id. *是你的觀點只是ID的。

要獲得真正的觀點,稱:

findViewById(R.id.nieubwel); 

在你的活動(的onPause和的onResume是罰款)。

findViewById返回一個普遍的看法,所以你必須將它轉換爲複選框:

(CheckBox)findViewById(R.id.nieubwel);