2016-07-22 24 views
0

我使用产生的自身(由Android工作室)SettingsActivity。 我创建了一个ListenerPreference变化重新启动应用程序,但由于某种原因,我不能启动从这个ListenerIntent如何在我的应用程序启动的意图在PreferenceChangeListener

下面是代码:

private static Preference.OnPreferenceChangeListener listener = new Preference.OnPreferenceChangeListener(){ 
    @Override 
    public boolean onPreferenceChange(Preference preference, Object o) { 
     SettingsActivity settingsActivity = new SettingsActivity(); 
     settingsActivity.restartApp(); 
     return true; 
    } 
}; 

和:

private void restartApp() { 
    Intent intent = new Intent(SettingsActivity.this, MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    SettingsActivity.this.finish(); 
    startActivity(intent); 
} 

我不得不创建一个新的实例来这个班,因为我把从静态方法非静态方法。

侦听器从此方法调用:

private static void bindPreferenceSummaryToValue(Preference preference) { 
    // Set the listener to watch for value changes. 
    preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener); 
    preference.setOnPreferenceChangeListener(listener); 

    // Trigger the listener immediately with the preference's 
    // current value. 
    sBindPreferenceSummaryToValueListener.onPreferenceChange(preference, 
      PreferenceManager 
        .getDefaultSharedPreferences(preference.getContext()) 
        .getString(preference.getKey(), "")); 
} 

而且这种方法被称为从这里:

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
public static class GeneralPreferenceFragment extends PreferenceFragment { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.pref_general); 
     setHasOptionsMenu(true); 

     bindPreferenceSummaryToValue(findPreference(getString(R.string.language_shared_pref_key))); 
    } 
} 

这是logcat的:

Process: appinventor.ai_itiel_maimon.Rubiks_cube, PID: 14454 
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference 
at android.app.Activity.startActivityForResult(Activity.java:4283) 
at android.app.Activity.startActivityForResult(Activity.java:4230) 
at android.app.Activity.startActivity(Activity.java:4567) 
at android.app.Activity.startActivity(Activity.java:4535) 
at appinventor.ai_itiel_maimon.Rubiks_cube.SettingsActivity.restartApp(SettingsActivity.java:59) 
at appinventor.ai_itiel_maimon.Rubiks_cube.SettingsActivity.access$000(SettingsActivity.java:16) 
at appinventor.ai_itiel_maimon.Rubiks_cube.SettingsActivity$2.onPreferenceChange(SettingsActivity.java:50) 
at android.preference.Preference.callChangeListener(Preference.java:1173) 
at android.preference.ListPreference.onDialogClosed(ListPreference.java:282) 
at android.preference.DialogPreference.onDismiss(DialogPreference.java:424) 
at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1533) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7229) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

回答

1

不要创建与new一个活动。将侦听器更改为非静态或使重新启动应用程序方法静态并使用Context参数。这只是糟糕的设计可能会导致意外的错误,你当然不能使用活动来调用startActivity

+0

我试图使重新启动应用程序的方法静态的,而是我如何使用上下文参数? –

+0

我会用除去了'onPreferenceChangesListener' – Muzikant

+0

了'static'开始和我不能改变听者不是静态的,因为它是必须是静态的静态方法调用。 –

相关问题