为Spinner修复了BadTokenException错误,但现在TextViews没有显示在布局中。我怎样才能解决这两个问题?

时间:2013-04-25 21:10:30

标签: android layout spinner

我有一个带微调器的对话框。

        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

            }
        });

当我点击Spinner时,我收到错误

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中的TextView。我该如何解决这两个问题?

修改

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

            }
        });

1 个答案:

答案 0 :(得分:0)

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

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

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