I'm having a strange problem which only exhibits itself on certain manufacturers phones. Specifically HTC, this runs fine on my Samsumg S2.
I have 2 methods:
public void setPref(String key) {
prefkey = key ;
}
public String getPref() {
return prefkey ;
}
I'm using them to store a shared preference name before I start an intent for image selection and then read the value back in the onActivityResult so I can actually save the image path to the right preference.
The problem is the setPref() and getPref() methods only appear to work within the individual method only. so when I setPref(preference.getKey()) in onPreferenceTreeClick the set works fine and I've done a println to show that a getPref() works within the method, but when I run the getPref() in the onActivityResult method, it just returns null (only on HTC phones) hence the catlog output also attached. My Samsung S2 works fine returning the correct value. Am I doing something wrong in my java? (NOTE: I've not included some of the working code to make it look a bit easier to read)
public class Preferences extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private String prefkey ;
private SharedPreferences preferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
// TODO Auto-generated method stub
if (preference.toString().substring(0,3).equals("Pho")) {
setPref(preference.getKey());
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(resultCode != RESULT_CANCELED){
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
SharedPreferences mySharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplication());
mySharedPrefs.edit().putString(getPref(),selectedImagePath).commit();
super.onActivityResult(requestCode, resultCode, data);
}
}
}
}
public void setPref(String key) {
prefkey = key ;
}
public String getPref() {
return prefkey ;
}
}
LogCat
08-14 07:05:53.371 I/System.out(19175): OnSharedPref: arg0android.app.SharedPreferencesImpl@40d843b0 arg1 null
08-14 07:05:53.371 D/AndroidRuntime(19175): Shutting down VM
08-14 07:05:53.371 W/dalvikvm(19175): threadid=1: thread exiting with uncaught exception (group=0x40a97a08)
08-14 07:05:53.381 E/EmbeddedLogger( 431): App crashed! Process: org.bazza.android.testlist
08-14 07:05:53.381 E/EmbeddedLogger( 431): App crashed! Package: org.bazza.android.testlist v1 (1.0)
08-14 07:05:53.381 E/EmbeddedLogger( 431): Application Label: testlist
08-14 07:05:53.381 E/AndroidRuntime(19175): FATAL EXCEPTION: main
08-14 07:05:53.381 E/AndroidRuntime(19175): java.lang.RuntimeException: Unable to resume activity {org.bazza.android.testlist/org.bazza.android.testlist.Preferences}: java.lang.RuntimeException: Failure
delivering result ResultInfo{who=null, request=1, result=-1, data=Intent {
dat=content://media/external/images/media/1297 typ=image/jpeg (has extras) }}
to activity {org.bazza.android.testlist/org.bazza.android.testlist.Preferences}: java.lang.NullPointerException
08-14 07:05:53.381 E/AndroidRuntime(19175): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2823)
08-14 07:05:53.381 E/AndroidRuntime(19175): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2862)
08-14 07:05:53.381 E/AndroidRuntime(19175): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-14 07:05:53.381 E/AndroidRuntime(19175): at android.app.ActivityThread.access$600(ActivityThread.java:139)
08-14 07:05:53.381 E/AndroidRuntime(19175): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
08-14 07:05:53.381 E/AndroidRuntime(19175): at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 07:05:53.381 E/AndroidRuntime(19175): at android.os.Looper.loop(Looper.java:154)
08-14 07:05:53.381 E/AndroidRuntime(19175): at android.app.ActivityThread.main(ActivityThread.java:4977)
08-14 07:05:53.381 E/AndroidRuntime(19175): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 07:05:53.381 E/AndroidRuntime(19175): at java.lang.reflect.Method.invoke(Method.java:511)
08-14 07:05:53.381 E/AndroidRuntime(19175): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-14 07:05:53.381 E/AndroidRuntime(19175): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-14 07:05:53.381 E/AndroidRuntime(19175): at dalvik.system.NativeStart.main(Native Method)
08-14 07:05:53.381 E/AndroidRuntime(19175): Caused by: java.lang.RuntimeException: Failure
delivering result ResultInfo{who=null, request=1, result=-1, data=Intent
{dat=content://media/external/images/media/1297 typ=image/jpeg (has extras) }} to
activity {org.bazza.android.testlist/org.bazza.android.testlist.Preferences}: java.lang.NullPointerException
08-14 07:05:53.381 E/AndroidRuntime(19175): at android.app.ActivityThread.deliverResults(ActivityThread.java:3398)
08-14 07:05:53.381 E/AndroidRuntime(19175): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2804)
08-14 07:05:53.381 E/AndroidRuntime(19175): ... 12 more
08-14 07:05:53.381 E/AndroidRuntime(19175): Caused by: java.lang.NullPointerException
08-14 07:05:53.381 E/AndroidRuntime(19175): at org.bazza.android.testlist.Preferences.onSharedPreferenceChanged(Preferences.java:172)
08-14 07:05:53.381 E/AndroidRuntime(19175): at android.app.SharedPreferencesImpl$EditorImpl.notifyListeners(SharedPreferencesImpl.java:455)
08-14 07:05:53.381 E/AndroidRuntime(19175): at android.app.SharedPreferencesImpl$EditorImpl.commit(SharedPreferencesImpl.java:441)
08-14 07:05:53.381 E/AndroidRuntime(19175): at org.bazza.android.testlist.Preferences.onActivityResult(Preferences.java:136)
08-14 07:05:53.381 E/AndroidRuntime(19175): at android.app.Activity.dispatchActivityResult(Activity.java:4747)
08-14 07:05:53.381 E/AndroidRuntime(19175): at android.app.ActivityThread.deliverResults(ActivityThread.java:3394)
08-14 07:05:53.381 E/AndroidRuntime(19175): ... 13 more
08-14 07:05:53.381 W/ActivityManager( 431): Force finishing activity org.bazza.android.testlist/.Preferences
08-14 07:05:53.411 D/ViewRootImpl( 431): @@@- disable SystemServer HW acceleration