【问题标题】:Save checkbox value保存复选框值
【发布时间】:2011-10-29 21:18:24
【问题描述】:

您好,我想以 .xml 形式保存一些复选框的值。 我在我的主 xml 上为此使用 SharedPreferences,但在收到消息时无法编译:

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

到目前为止的代码:

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

【问题讨论】:

  • 我徘徊有两个复选框声明,其中一个是最终的!为什么需要在 OnPause 中再次声明复选框?
  • 嗨 Walid,我发布了带有复选框的布局 xml 文件,您能解释为什么它不起作用吗?
  • NullPointerExceptions 是最容易找出的异常。这意味着某事为空(这可能意味着它已声明,但未实例化)。最有可能的是 nieubwel 为空。请阅读一些入门的 Android 教程 (developer.android.com/resources/tutorials/notepad/index.html)。
  • 嗨,杰克,是的,我知道,我已经尝试过everuthing,但我无法摆脱空指针,你能看看吗?非常感谢。请在我的代码中查看编辑,有什么问题?

标签: android checkbox sharedpreferences


【解决方案1】:

您无法使用 R.id.nieuwbel.isChecked() 访问复选框的值。方法如下:

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

【讨论】:

  • 嗨杰克,谢谢你的回答,如果我应用你的更正,我会强制关闭。查看我编辑的异常代码。
  • 请从 logcat 发布异常日志。
  • 复选框必须为空。请参阅stackoverflow.com/questions/4716482/…。它基本上说将您的复选框移动到类级别。
  • 嗨,杰克,更改了代码,但仍然是异常,请查看编辑后的代码。
  • 您需要相应地更改您的方法。而不是最终的 CheckBox checkBox = (CheckBox) findViewById(R.id.nieuwbel);应该是 checkBox = (CheckBox) findViewById(R.id.nieuwbel);
【解决方案2】:

R.id.* 只是您的视图的 ID。

要获得真实视图,请调用:

findViewById(R.id.nieubwel);

在您的活动中(onPause 和 onResume 都可以)。

findViewById 返回一般视图,因此您必须将其强制转换为复选框:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 2019-02-11
    • 2021-01-10
    • 2017-04-17
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多