1

Hi i want to save the values, of some checkboxes i have in a .xml form. I use the SharedPreferences for this on my main xml but i can not compile as i get the message:

Cannot invoke isChecked() on the primitive type int

Can somebody help me please? How should the code look like?

Edited code:

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

Exception code 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

Edit 2: Almost solved the issue together with another user but when i start the app and close it without doing anything (show menu etc.) i`ll get a nullexception on this line in onPause:

putBoolean(PREF_BOOL, nieuwbel.isChecked()); 

The code so far:

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

Calling the layout xml via a menu:

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

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

Edit 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();    
}
Lars
  • 915
  • 4
  • 18
  • 27
  • I wander there's two declaration for the checkbox, and One of them is final!! Why do you need to declare the checkbox again in OnPause? – Walid Hossain Oct 30 '11 at 07:38
  • Hi Walid, I posted my layout xml file with the checkboxes, do you have an explanation why it is not working? – Lars Oct 30 '11 at 12:28
  • NullPointerExceptions are the easiest of the exceptions to figure out. It means something is null (which might mean its declared, but not instantiated). It is most likely nieubwel that is null. Please read up on some of the beginning Android tutorials (http://developer.android.com/resources/tutorials/notepad/index.html). – Jack Oct 30 '11 at 17:53
  • Hi Jack, yes, i know, i already tried everuthing but i can`t get rid of the nullpointer could you please have a look? Thank you very much. PLease see Edit in my code, what is wrong? – Lars Oct 31 '11 at 21:23

2 Answers2

2

You cannot access a checkbox's value by using R.id.nieuwbel.isChecked(). Here is how:

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

See this link.

So onPause would look like this:

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

and onResume would look like this:

@Override 
public void onResume() { 
    super.onResume(); 
    final CheckBox checkBox = (CheckBox) findViewById(R.id.nieuwbel);
    checkBox.setChecked(load());
} 
Jack
  • 9,156
  • 4
  • 50
  • 75
  • Hi Jack, Thx for answering, if i apply your corrections, i`ll get a forceclose. See my edited code for exception. – Lars Oct 29 '11 at 21:38
  • Please post the exception log from logcat. – Jack Oct 29 '11 at 21:42
  • checkBox must be null. Please see http://stackoverflow.com/questions/4716482/android-how-can-i-access-a-view-object-instantiated-in-oncreate-in-onresume. It basically says move your checkBox to the class level. – Jack Oct 29 '11 at 21:55
  • Hi Jack, Changed the code but still the exception, please see edited code. – Lars Oct 29 '11 at 22:07
  • You need to change your methods accordingly. Instead of final CheckBox checkBox = (CheckBox) findViewById(R.id.nieuwbel); it should be checkBox = (CheckBox) findViewById(R.id.nieuwbel); – Jack Oct 29 '11 at 22:10
  • Hi Jack, I did that already, but it keeps coming, see my edited code. – Lars Oct 29 '11 at 22:13
  • Then I have no idea why that is happening. See http://stackoverflow.com/questions/4436035/wanted-an-example-which-uses-onresume-on-start-and-on-restart. At the class level have your checkBox. In onCreate call checkBox = (CheckBox) findViewById(R.id...), then in onResume you should just be able to call checkBox.setChecked(...). – Jack Oct 29 '11 at 22:27
  • Lars, your checkbox is null in your other question too. http://stackoverflow.com/questions/7942743/nullpointerexception-checkbox-sharedpreferences You need to post your layout XML, as there must be a problem with the findViewById(R.id.nieuwbel) – Dan J Oct 30 '11 at 03:59
  • Hi Dan J, I will post the layout of the layout xml, see edited code. Thx again for looking into this. – Lars Oct 30 '11 at 09:52
  • Hi Jack, I tried to solve it together with another user, there only remains 1 error, when the app startet and i immediatly after that close it, gives a NullpointerException. Please see edited code from me. – Lars Oct 30 '11 at 17:34
1

R.id.* are just ID's of your views.

To get the real view, call:

findViewById(R.id.nieubwel);

In your activity (onPause and onResume are fine).

findViewById returns a general view, so you must cast it to check box:

(CheckBox)findViewById(R.id.nieubwel);
Jong
  • 9,045
  • 3
  • 34
  • 66