Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid SDK SharedPreferences troubles
    primarykey
    data
    text
    <p>I'm working on an app and would like to save the state of obe of my checkbox options, but when I try using the SharedPreferences class (first time using it) I get a nullpointer exception. I have been troubleshooting for a couple hours and cant find a solution. Could anyone look at this and tell me what's wrong? The full code is alot longer but this is the park giving me the null pointer exception. I know for a fact it has to do with SharedPreferences.</p> <pre><code>package us.mattmccoy.meanfind; //import java.util.Arrays; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.content.ClipData; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { //saved data stuff needed SharedPreferences preferences = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE); Editor edit = preferences.edit(); //private data using saved data String autoclearKEY = "us.mattmccoy.meanfind.AUTOCLEAR"; boolean autoclear = preferences.getBoolean(autoclearKEY, false); //normal private data final Context con1 = this; int dividend;String dataFixed;boolean tb; private CheckBox myCBox, acBox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); addListenerBoxes(); if(autoclear == true){ acBox.setChecked(true); }else{ acBox.setChecked(false); } } //check box listeners public void addListenerBoxes(){ //instantiate boxes acBox = (CheckBox) findViewById(R.id.chex2); acBox.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //is acBox checked? if (((CheckBox) v).isChecked()) { edit.putBoolean(autoclearKEY, true).commit(); }else{ edit.putBoolean(autoclearKEY, false).commit(); } } }); myCBox = (CheckBox) findViewById(R.id.chex); } </code></pre> <p>error message:</p> <p><a href="http://pastebin.com/5P2Mfwik" rel="nofollow noreferrer">http://pastebin.com/5P2Mfwik</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload