TabActivity 与 ActivityGroup 和 PreferenceActivity 子级

发布于 2024-12-08 20:11:00 字数 1454 浏览 0 评论 0原文

我有一个 TabActivity,其中每个选项卡都有 ActivityGroup。 在第一组的主 ActivityChild 上,我有一个菜单选项,它为用户提供了打开首选项的选项。 当我单击菜单上的“首选项”时,我会在 ActivityGroup 内启动 PreferenceActivity,这会在第一个选项卡上显示 PreferenceActivity。 问题是当我单击任何必须显示对话框(用于 EditTextPreference)的特定首选项时。 我有以下例外:

android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@405d3a20

我知道问题是因为 PreferenceActivity 显示的对话框使用了错误的上下文,但我现在不知道如何更改创建的对话框的上下文。

下面是我创建的 PreferenceActivity。

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) {     

    }   
}

我不想创建自定义对话框。我想为此使用 PreferenceActivity 机制。 下面是我用来添加到组的代码:

i = new Intent(MyActivity.this, PreferencesActivity.class);
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.startChildActivity("PreferencesActivity", i);

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦过后 2024-12-15 20:11:00
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文