Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Why is This Crashing?!

  • 24-09-2012 03:21PM
    #1
    Registered Users, Registered Users 2 Posts: 8,324 ✭✭✭


    Hey,

    Just new to this so probably missing something obvious. I'm just trying to get SharedPreferences to work. I think the code theory is sound, but every time I press the button that opens it, the app crashes. The debug info doesn't really help.

    This is the code

    MAIN SCREEN (Just a button for the moment)
    package cs4084.panic.button;
    
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    
    public class MainScreen extends Activity {
    	
    	
    	// opening settings activity from main screen - only visible from main screen
    	 public void openSettings(View view)
    	    {
    	    	
    	    	Intent intent = new Intent (this, Settings.class);
    	    	startActivity(intent);
    	    	
    	    }
    	 // end comment
    	 
    	 
    	 // opening ConfirmPanic activity after initial press of panic button
    	 public void openConfirmPanic(View view)
    	 {
    		 
    		 Intent intent = new Intent (this, ConfirmPanic.class);
    		 startActivity(intent);
    		 
    	 }
    	 // end
    	 
    	 
    	 
    	 
    	 
    	 
    	
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main_screen);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main_screen, menu);
            return true;
        }
    }
    



    SETTINGS CODE
    package cs4084.panic.button;
    
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.EditText;
    
    public class Settings extends Activity implements OnClickListener{
    	
    	CheckBox location;
    	EditText saveName;
    	Button save;
    	
    	@Override
    	public void onCreate(Bundle savedInstanceState){
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_settings);
    		loadPrefs();
    		location = (CheckBox)findViewById(R.id.checkLocation);
    		saveName = (EditText)findViewById(R.id.name);
    		save = (Button)findViewById(R.id.saveButton);
    		save.setOnClickListener(this);		
    		
    	}
    	
    	
    	private void savePrefs(String key, boolean value)
    	{
    		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); 
    		Editor edit = sp.edit();
    		edit.putBoolean(key, value);
    		edit.commit();
    		 
    	}
    	
    	private void savePrefs(String key, String value)
    	{
    		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); 
    		Editor edit = sp.edit();
    		edit.putString(key, value);
    		edit.commit();
    	}
    	
    	private void loadPrefs()
    	{
    		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); 
    		boolean locationValue = sp.getBoolean("LOCATION", false);
    		String nameValue = sp.getString("NAME", "<no name found>");
    		if (locationValue)
    		{
    			location.setChecked(true);
    		}
    		else 
    			{
    				location.setChecked(false);
    			}
    		
    		saveName.setText(nameValue);	
    	}
    	
    	public void onClick (View v)
    	{
    		savePrefs("LOCATION",location.isChecked()); 
    		savePrefs("NAME", saveName.getText().toString());
    		finish();
    	}
    	
    }
    





    DEBUG INFO
    see attached file

    Any help would be appreciated as I had it working with a non working version of the settings.


Comments

  • Registered Users, Registered Users 2 Posts: 2,347 ✭✭✭Kavrocks


    09-24 15:20:14.463: E/AndroidRuntime(12260): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	... 11 more
    

    That is all I see in your debug info which isn't helpful. There should be more a lot more like the specific exception that was thrown.


  • Registered Users, Registered Users 2 Posts: 8,324 ✭✭✭chrislad


    Try this. Sorry!
    09-24 15:20:14.458: W/dalvikvm(12260): threadid=1: thread exiting with uncaught exception (group=0x41e35300)
    09-24 15:20:14.463: E/AndroidRuntime(12260): FATAL EXCEPTION: main
    09-24 15:20:14.463: E/AndroidRuntime(12260): java.lang.RuntimeException: Unable to start activity ComponentInfo{cs4084.panic.button/cs4084.panic.button.Settings}: java.lang.NullPointerException
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	at android.app.ActivityThread.access$600(ActivityThread.java:140)
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	at android.os.Handler.dispatchMessage(Handler.java:99)
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	at android.os.Looper.loop(Looper.java:137)
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	at android.app.ActivityThread.main(ActivityThread.java:4898)
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	at java.lang.reflect.Method.invokeNative(Native Method)
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	at java.lang.reflect.Method.invoke(Method.java:511)
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	at dalvik.system.NativeStart.main(Native Method)
    09-24 15:20:14.463: E/AndroidRuntime(12260): Caused by: java.lang.NullPointerException
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	at cs4084.panic.button.Settings.loadPrefs(Settings.java:69)
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	at cs4084.panic.button.Settings.onCreate(Settings.java:27)
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	at android.app.Activity.performCreate(Activity.java:5184)
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
    09-24 15:20:14.463: E/AndroidRuntime(12260): 	... 11 more
    


  • Registered Users, Registered Users 2 Posts: 2,347 ✭✭✭Kavrocks


    09-24 15:20:14.463: E/AndroidRuntime(12260): Caused by: java.lang.NullPointerException 09-24 15:20:14.463:
    E/AndroidRuntime(12260): at cs4084.panic.button.Settings.loadPrefs(Settings.java:69)
    

    That shows you what you need.


  • Registered Users, Registered Users 2 Posts: 8,324 ✭✭✭chrislad


    Kavrocks wrote: »
    09-24 15:20:14.463: E/AndroidRuntime(12260): Caused by: java.lang.NullPointerException 09-24 15:20:14.463:
    E/AndroidRuntime(12260): at cs4084.panic.button.Settings.loadPrefs(Settings.java:69)
    

    That shows you what you need.

    Cheers. Will have a look tomorrow. Between this and OOD, I'm all coded out for the day.

    Edit: Got it working! Stupid mis-placed method call!


Advertisement