我有一个奇怪的问题,它只出现在某些制造商的手机上。特别是 HTC,这在我的 Samsumg S2 上运行良好。
我有两种方法:
public void setPref(String key) {
prefkey = key ;
}
public String getPref() {
return prefkey ;
}
在开始图像选择的意图之前,我使用它们来存储共享首选项名称,然后在 onActivityResult 中读回该值,以便我可以实际将图像路径保存到正确的首选项。
问题是 setPref() 和 getPref() 方法似乎只能在单个方法中工作。因此,当我在 onPreferenceTreeClick 中 setPref(preference.getKey()) 时,该集合工作正常,并且我已经完成了 println 以显示 getPref() 在该方法中有效,但是当我在 onActivityResult 方法中运行 getPref() 时,它只返回 null(仅在 HTC 手机上),因此还附加了 catlog 输出。我的三星 S2 工作正常,返回正确的值。我在我的java中做错了吗?(注意:我没有包含一些工作代码以使其看起来更容易阅读)
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 ;
}
}
日志猫
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