问题
I have a TabActivity where each tab has ActivityGroup. On the home ActivityChild of the first group I have an menu option, which gives to the user the option to open preferences. When I click "Preferences" on menu, I start PreferenceActivity inside ActivityGroup, which shows PreferenceActivity on the first tab. The problem is when I click on any specific preference which has to show a Dialog (for EditTextPreference). I have the following exception:
android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@405d3a20
I understand that the problem is because Dialog to be shown by PreferenceActivity uses wrong context, BUT i don't now how change the context of created dialog.
Belows is the PreferenceActivity I've created.
public class PreferencesActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preferences);
addPreferencesFromResource(R.xml.preferences);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
prefs.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {
}
}
I don't want to create custom dialogs. I want to use the mechanism of PreferenceActivity for that. Below is the code that I'm using to add to group:
i = new Intent(MyActivity.this, PreferencesActivity.class);
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.startChildActivity("PreferencesActivity", i);
Any ideas?
回答1:
This is very common problem with dialog's in Tab Host.
Actually the Activity context is not sufficient to show a Dialog in Tab.
You have to use the context of your GroupActivity for the dialog to be enabled without exception
回答2:
I solved this problem with a bad solution , here unfortunately the only way I could.
来源:https://stackoverflow.com/questions/7716227/tabactivity-with-activitygroup-and-preferenceactivity-child