我尝试使用Google Settings | Android Developers中的指南为我的应用设置设置。 但每当我尝试打开设置时,它就说不幸已经停止了。
MainActivity.java
...
Intent startSettings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
setContentView(R.layout.main);
startSettings = new Intent(this, SettingsActivity.class);
}
public void openSettings() {
startActivity(startSettings);
}
...
SettingsActivity.java
...
public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
}
SettingsFragment.java
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// Load preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
的preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/main_settings_title"
android:key="pref_key_main_settings">
<SwitchPreference
android:key="pref_key_enable_service"
android:summary="@string/pref_summary_enable_service"
android:title="@string/pref_title_enable_service"
android:defaultValue="false" />
</PreferenceCategory>
</PreferenceScreen>
Logcat错误
07-09 11:37:24.355 26218-26218/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ilolabs.ctdn/com.ilolabs.ctdn.SettingsActivity}: java.lang.RuntimeException: This should be called after super.onCreate.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2072)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2097)
at android.app.ActivityThread.access$600(ActivityThread.java:137)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4863)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: This should be called after super.onCreate.
at android.preference.PreferenceFragment.requirePreferenceManager(PreferenceFragment.java:318)
at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:283)
at com.ilolabs.ctdn.SettingsFragment.onCreate(SettingsFragment.java:14)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:796)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
at android.app.BackStackRecord.run(BackStackRecord.java:635)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
at android.app.Activity.performStart(Activity.java:5249)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2097)
at android.app.ActivityThread.access$600(ActivityThread.java:137)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4863)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
我试着阅读这个logcat并搜索我的代码中的错误,但我找不到对我有意义的代码崩溃的原因。我还没有太多经验,我是新手。请指出我哪里做错了?我完全按照上面链接的谷歌指南向我解释了。
答案 0 :(得分:1)
您忘记在SettingsFragment的onCreate方法中致电arr = ['next_release()', 'last_release("com_disagg")']
class t(object):
def head(self):
print "inside head"
return "head printed"
def next_release():
print "nr"
return t()
def last_release(var):
print var
return t()
for i in arr:
eval(i)
for i in arr:
print eval(i+".head()")
for i in arr:
print eval(i).head()
。看看最后一个&#34;引起的&#34;异常堆栈跟踪中的行。
答案 1 :(得分:0)
请看这个 -
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Load preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}