PreferenceActivity 在方向更改时崩溃
PreferenceActivity Crashes on Orientation Change
我正在写一个 PreferenceActivity
和一个 PreferenceFragment
,并且当设备的方向改变时抛出一个 InstantiationException
(即横向到纵向,反之亦然)。此外,如果我在弹出的对话框 ("Unfortunately, YourApp has stopped.") 上点击 'Ok',主 activity 将重新启动,并且 PreferenceActivity
在新方向上完美运行,直到它被更改再次。我似乎无法在网上找到任何关于此的信息。
堆栈跟踪:
05-26 09:28:10.860: E/AndroidRuntime(3439): FATAL EXCEPTION: main
05-26 09:28:10.860: E/AndroidRuntime(3439): Process: com.example.myapp, PID: 3439
05-26 09:28:10.860: E/AndroidRuntime(3439): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.SettingsActivity}: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.devgregw.strikedistance_androidphoneandtablet.SettingsActivity: make sure class name exists, is public, and has an empty constructor that is public
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.ActivityThread.access0(ActivityThread.java:135)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.os.Handler.dispatchMessage(Handler.java:102)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.os.Looper.loop(Looper.java:136)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.ActivityThread.main(ActivityThread.java:5027)
05-26 09:28:10.860: E/AndroidRuntime(3439): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 09:28:10.860: E/AndroidRuntime(3439): at java.lang.reflect.Method.invoke(Method.java:515)
05-26 09:28:10.860: E/AndroidRuntime(3439): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-26 09:28:10.860: E/AndroidRuntime(3439): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-26 09:28:10.860: E/AndroidRuntime(3439): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
05-26 09:28:10.860: E/AndroidRuntime(3439): at dalvik.system.NativeStart.main(Native Method)
05-26 09:28:10.860: E/AndroidRuntime(3439): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.myapp.SettingsActivity: make sure class name exists, is public, and has an empty constructor that is public
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.Fragment.instantiate(Fragment.java:601)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.FragmentState.instantiate(Fragment.java:98)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1759)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.Activity.onCreate(Activity.java:899)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.preference.PreferenceActivity.onCreate(PreferenceActivity.java:514)
05-26 09:28:10.860: E/AndroidRuntime(3439): at com.example.myapp.SettingsActivity.onCreate(SettingsActivity.java:23)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.Activity.performCreate(Activity.java:5231)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-26 09:28:10.860: E/AndroidRuntime(3439): ... 13 more
05-26 09:28:10.860: E/AndroidRuntime(3439): Caused by: java.lang.InstantiationException: can't instantiate class com.example.myapp.SettingsActivity; no empty constructor
05-26 09:28:10.860: E/AndroidRuntime(3439): at java.lang.Class.newInstanceImpl(Native Method)
05-26 09:28:10.860: E/AndroidRuntime(3439): at java.lang.Class.newInstance(Class.java:1208)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.Fragment.instantiate(Fragment.java:590)
05-26 09:28:10.860: E/AndroidRuntime(3439): ... 21 more
SettingsActivity.java
:
package com.example.myapp;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceGroup;
public class SettingsActivity extends PreferenceActivity {
public OnSharedPreferenceChangeListener changeListener;
public SharedPreferences preferences;
public PreferenceFragment fragment;
public SettingsActivity() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
changeListener = new OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
Preference preference = fragment.findPreference(key);
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
listPreference.setSummary(listPreference.getEntry());
}
}
};
fragment = new PreferenceFragment() {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
preferences = getPreferenceScreen().getSharedPreferences();
}
@Override
public void onResume() {
super.onResume();
for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); ++i) {
Preference preference = getPreferenceScreen()
.getPreference(i);
if (preference instanceof PreferenceGroup) {
PreferenceGroup preferenceGroup = (PreferenceGroup) preference;
for (int j = 0; j < preferenceGroup
.getPreferenceCount(); ++j) {
updatePreference(preferenceGroup.getPreference(j));
}
} else {
updatePreference(preference);
}
}
}
public void updatePreference(Preference preference) {
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
listPreference.setSummary(listPreference.getEntry());
}
}
};
getFragmentManager().beginTransaction()
.replace(android.R.id.content, fragment).commit();
}
@Override
protected void onResume() {
super.onResume();
preferences.registerOnSharedPreferenceChangeListener(changeListener);
}
@Override
protected void onPause() {
super.onPause();
preferences.unregisterOnSharedPreferenceChangeListener(changeListener);
}
}
你的Fragment
应该有一个构造函数
Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.myapp.SettingsActivity: make sure class name exists, is public, and has an empty constructor that is public
同时将 Fragment
声明作为 static 内部 class 或另一个 .java 文件中的声明。
有关详细信息,您可以阅读文档 here. Or you can take a look at the Android training guide about Fragments here。
我正在写一个 PreferenceActivity
和一个 PreferenceFragment
,并且当设备的方向改变时抛出一个 InstantiationException
(即横向到纵向,反之亦然)。此外,如果我在弹出的对话框 ("Unfortunately, YourApp has stopped.") 上点击 'Ok',主 activity 将重新启动,并且 PreferenceActivity
在新方向上完美运行,直到它被更改再次。我似乎无法在网上找到任何关于此的信息。
堆栈跟踪:
05-26 09:28:10.860: E/AndroidRuntime(3439): FATAL EXCEPTION: main
05-26 09:28:10.860: E/AndroidRuntime(3439): Process: com.example.myapp, PID: 3439
05-26 09:28:10.860: E/AndroidRuntime(3439): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.SettingsActivity}: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.devgregw.strikedistance_androidphoneandtablet.SettingsActivity: make sure class name exists, is public, and has an empty constructor that is public
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.ActivityThread.access0(ActivityThread.java:135)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.os.Handler.dispatchMessage(Handler.java:102)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.os.Looper.loop(Looper.java:136)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.ActivityThread.main(ActivityThread.java:5027)
05-26 09:28:10.860: E/AndroidRuntime(3439): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 09:28:10.860: E/AndroidRuntime(3439): at java.lang.reflect.Method.invoke(Method.java:515)
05-26 09:28:10.860: E/AndroidRuntime(3439): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-26 09:28:10.860: E/AndroidRuntime(3439): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-26 09:28:10.860: E/AndroidRuntime(3439): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
05-26 09:28:10.860: E/AndroidRuntime(3439): at dalvik.system.NativeStart.main(Native Method)
05-26 09:28:10.860: E/AndroidRuntime(3439): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.myapp.SettingsActivity: make sure class name exists, is public, and has an empty constructor that is public
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.Fragment.instantiate(Fragment.java:601)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.FragmentState.instantiate(Fragment.java:98)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1759)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.Activity.onCreate(Activity.java:899)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.preference.PreferenceActivity.onCreate(PreferenceActivity.java:514)
05-26 09:28:10.860: E/AndroidRuntime(3439): at com.example.myapp.SettingsActivity.onCreate(SettingsActivity.java:23)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.Activity.performCreate(Activity.java:5231)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-26 09:28:10.860: E/AndroidRuntime(3439): ... 13 more
05-26 09:28:10.860: E/AndroidRuntime(3439): Caused by: java.lang.InstantiationException: can't instantiate class com.example.myapp.SettingsActivity; no empty constructor
05-26 09:28:10.860: E/AndroidRuntime(3439): at java.lang.Class.newInstanceImpl(Native Method)
05-26 09:28:10.860: E/AndroidRuntime(3439): at java.lang.Class.newInstance(Class.java:1208)
05-26 09:28:10.860: E/AndroidRuntime(3439): at android.app.Fragment.instantiate(Fragment.java:590)
05-26 09:28:10.860: E/AndroidRuntime(3439): ... 21 more
SettingsActivity.java
:
package com.example.myapp;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceGroup;
public class SettingsActivity extends PreferenceActivity {
public OnSharedPreferenceChangeListener changeListener;
public SharedPreferences preferences;
public PreferenceFragment fragment;
public SettingsActivity() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
changeListener = new OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
Preference preference = fragment.findPreference(key);
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
listPreference.setSummary(listPreference.getEntry());
}
}
};
fragment = new PreferenceFragment() {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
preferences = getPreferenceScreen().getSharedPreferences();
}
@Override
public void onResume() {
super.onResume();
for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); ++i) {
Preference preference = getPreferenceScreen()
.getPreference(i);
if (preference instanceof PreferenceGroup) {
PreferenceGroup preferenceGroup = (PreferenceGroup) preference;
for (int j = 0; j < preferenceGroup
.getPreferenceCount(); ++j) {
updatePreference(preferenceGroup.getPreference(j));
}
} else {
updatePreference(preference);
}
}
}
public void updatePreference(Preference preference) {
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
listPreference.setSummary(listPreference.getEntry());
}
}
};
getFragmentManager().beginTransaction()
.replace(android.R.id.content, fragment).commit();
}
@Override
protected void onResume() {
super.onResume();
preferences.registerOnSharedPreferenceChangeListener(changeListener);
}
@Override
protected void onPause() {
super.onPause();
preferences.unregisterOnSharedPreferenceChangeListener(changeListener);
}
}
你的Fragment
应该有一个构造函数
Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.myapp.SettingsActivity: make sure class name exists, is public, and has an empty constructor that is public
同时将 Fragment
声明作为 static 内部 class 或另一个 .java 文件中的声明。
有关详细信息,您可以阅读文档 here. Or you can take a look at the Android training guide about Fragments here。