特定のメーカーの電話でしか表示されない奇妙な問題が発生しています。特にHTC、これは私のサムスンS2でうまく動作します。
私には2つの方法があります:
public void setPref(String key) {
prefkey = key ;
}
public String getPref() {
return prefkey ;
}
画像選択のインテントを開始する前に、それらを使用して共有設定名を保存し、onActivityResultに値を読み戻して、実際に画像パスを正しい設定に保存できるようにします。
問題は、setPref()メソッドとgetPref()メソッドが個々のメソッド内でのみ機能するように見えることです。したがって、onPreferenceTreeClickでsetPref(preference.getKey())を実行すると、セットは正常に機能し、getPref()がメソッド内で機能することを示すためにprintlnを実行しましたが、onActivityResultメソッドでgetPref()を実行すると、 nullを返すだけなので(HTC電話のみ)、catlog出力も添付されます。私のSamsungS2は正常に動作し、正しい値を返します。私は自分の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 ;
}
}
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