我有一个列表首选项,如下所示
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<ListPreference android:key="userType"
android:title="User Type"
android:summary="This preference allows to select an item in a array"
android:defaultValue="Admin"
android:entries="@array/array_preference_userType"
android:entryValues="@array/array_preference_userTypeValues" />
</PreferenceScreen>
Prefs 类是
public class PreferencesActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_user_settings);
}
}
上面的课程在打开的时候被添加到活动组中。
Intent intent = new Intent(getParent(), PreferencesActivity.class);
ParentActivity parentActivity = (ParentActivity)getParent();
parentActivity.startChildActivity("PreferencesActivity", intent);
ParentActivity 类是:
public class ParentActivity extends ActivityGroup {
private ArrayList<String> mIdList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mIdList == null) mIdList = new ArrayList<String>();
}
public void startChildActivity(String Id, Intent intent) {
Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
if (window != null) {
Log.i("ParentActivity", "activity started: " + Id);
mIdList.add(Id);
setContentView(window.getDecorView());
}
}
}
}
但是当我单击列表首选项时,它会因错误而崩溃
E/AndroidRuntime(581): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@44f862a8 is not valid; is your activity running?
我可能会想到一个添加父上下文的解决方案,这是我在创建意图时所做的。
有什么想法吗?
提前致谢!