0

我有一个与微调器的对话。

        AlertDialog.Builder customDialog = new AlertDialog.Builder(this);
        LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.jquery_dialog, null);
        final EditText idTxt = (EditText) view.findViewById(R.id.idName);
        final Spinner themeSpinner = (Spinner) view
                .findViewById(R.id.themeSpinner);
        final CheckBox headerChk = (CheckBox) view.findViewById(R.id.headerChk);
        final CheckBox footerChk = (CheckBox) view.findViewById(R.id.footerChk);
        final RadioGroup group = (RadioGroup) view
                .findViewById(R.id.jqmNavigation);
        customDialog.setView(idTxt);
        customDialog.setView(headerChk);
        customDialog.setView(footerChk);
        themeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                themes = getResources().getStringArray(R.array.themes);
                theme = themes[arg2];
                Toast.makeText(NewFile.this, theme, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

当我单击微调器时,我收到错误

04-25 18:01:56.299: E/AndroidRuntime(21639331): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?

我通过将 LayoutInflater 代码更改为

LayoutInflater layoutInflater = LayoutInflater.from(MyActivity.this);

它修复了我的 Spinner 错误,但现在我的 Dialog 中的 TextViews 都没有出现。如何解决这两个问题?

编辑

public void jqueryMobileDialog() {
        AlertDialog.Builder customDialog = new AlertDialog.Builder(this);
        LayoutInflater layoutInflater = context.getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.jquery_dialog, null);
        final EditText idTxt = (EditText) view.findViewById(R.id.idName);
        final Spinner themeSpinner = (Spinner) view
                .findViewById(R.id.themeSpinner);
        final CheckBox headerChk = (CheckBox) view.findViewById(R.id.headerChk);
        final CheckBox footerChk = (CheckBox) view.findViewById(R.id.footerChk);
        final RadioGroup group = (RadioGroup) view
                .findViewById(R.id.jqmNavigation);
        customDialog.setView(idTxt);
        customDialog.setView(headerChk);
        customDialog.setView(footerChk);
        themeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                themes = getResources().getStringArray(R.array.themes);
                theme = themes[arg2];
                Toast.makeText(NewFile.this, theme, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
4

1 回答 1

0

布局充气器需要来自有权绘制到屏幕的上下文。只有当前活动有这个。所以调用 getApplicationContext().getLayoutInflater 永远不会工作-应用程序上下文没有权限。请改用当前活动的 getLayoutInflater。

还有另外两次可能发生这种情况。一种是如果您太快地弹出对话框(在您的活动有权显示之前)。我找到的唯一解决方案是在发生这种情况时向自己发送一条消息以在 100 毫秒内重试,然后重试几次。例如,如果您尝试在 onCreate 中显示您的对话框,就会发生这种情况。

另一种是当您的对话框试图同时显示您的活动失去前景时。对此没有解决方案,因为您不知道是否/何时再次成为前台应用程序。在这里你想捕捉和忽略。请注意,这也可能发生在轮换和其他终止和重新启动活动的配置更改周围。

于 2013-04-25T21:20:56.987 回答