Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid SharedPreferences crashes when getting values in a custom view
    primarykey
    data
    text
    <p>I've been looking around for awhile but no one really seems to have the same problem so maybe its just a simple programming error.</p> <p>From a fresh install on my tablet the program loads, then if you go to the preference menu and select the nPref text box and enter a value the program is fine. But when you restart the program it crashes initially after a value has been entered.</p> <p>A rough outline of my program looks like this:</p> <pre><code> &lt;import statments&gt; public class Galaxy extends Activity{ public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //request fullscreen super.onCreate(savedInstanceState); setContentView(new Simulation(this)); //set contentview to my class } public boolean onCreateOptionsMenu(Menu menu) { //create ("inflate") the menu MenuInflater inflater = getMenuInflater(); inflater.inflate(R.layout.options_menu, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { //check if a menu item is pressed switch (item.getItemId()) { case R.id.m_SETTINGS: Toast.makeText(Galaxy_test2Activity.this, "Preference Launched!",Toast.LENGTH_SHORT); Intent i = new Intent(Galaxy_test2Activity.this, Preferences.class); startActivity(i); return true; default: return super.onOptionsItemSelected(item); } } class Simulation extends View { //I declare my program variables here public Simulation(Context context) { super(context); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); //get the preferences int storedPreference = preferences.getInt("nPref", 0); //attempt to get a value from the preferences Log.i(TAG, "storedPref nPref = " + storedPreference); //log it to see if it is working //rest of my program //I also have onDraw and onTouchEvent } } } </code></pre> <p>If you need anything thing else I can post it</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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