【问题标题】:Click on spinner gives WindowManager$BadTokenException单击微调器会给出 WindowManager$BadTokenException
【发布时间】:2012-02-03 09:07:12
【问题描述】:

我正在使用 Tabwidget,Tab 的实现方式与 tutorial 中显示的方式相同

“MainActivity -> Activity1 -> Activity2(给定图像)”

现在点击微调器会给出

 android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@44eb8748 is not valid; is your activity running?

我已经尝试过Android Spinner Error : android.view.WindowManager$BadTokenException: Unable to add window,但这对我没有帮助

我知道上下文有问题,但我不知道是什么

听到是我的意图,开始于DetailActivity

intent = new Intent(getParent(), DetailActivity.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("SelectActivity", intent);

DetailActivity 的代码如下所示

@Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.package_detail);
        setUpViews();

        id = getIntent().getExtras().getInt("WEBSITE_ID");

        adapter = new KeywordAdapter(getApplicationContext(), id,
                getLNApplication().getKeyworddetail());
        listTags.setAdapter(adapter);

        spinneAdapter = new SpinnerListAdapter();
        spinnerList.setAdapter(spinneAdapter);
        spinnerList.setSelection(id, true);
        spinnerList
                .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    public void onItemSelected(AdapterView<?> parent,
                            View view, int position, long id) {

                        // txtHeader.setText(getLNApplication().getWebsiteList()
                        // .get(position).getName());
                        adapter.forceReload();
                        adapter = new KeywordAdapter(DetailActivity.this,
                                position, getLNApplication().getKeyworddetail());
                        listTags.setAdapter(adapter);
                    }

                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });

    }

SpinnerAdapter的代码

public class SpinnerListAdapter extends BaseAdapter {

        private List<ClientDetail> siteList;

        public SpinnerListAdapter() {
            siteList = getLNApplication().getWebsiteList();
        }
        
        public void forceReload() {
            notifyDataSetChanged();
            
        }

        @Override
        public int getCount() {
            return siteList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return siteList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            
//          LayoutInflater inflater = (LayoutInflater) context
//              .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
//          LayoutInflater inflater = getLayoutInflater();
    
//          LayoutInflater inflater = LayoutInflater.from(DetailActivity.this.getParent());
    
            LayoutInflater inflater = LayoutInflater.from(DetailActivity.this.getParent());
                view = inflater.inflate(R.layout.spinner_item_display, null);
                TextView websiteName = (TextView) view.findViewById(R.id.spinnerItem);
            if (siteList.get(position).getName() != null) {
                websiteName.setText(siteList.get(position).getName());
                websiteName.setTextColor(0xFF000000);
            }
            return view;
        }

        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            
//          LayoutInflater inflater = (LayoutInflater) context
//                  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            
//          LayoutInflater inflater = getLayoutInflater();
            
//          LayoutInflater inflater = LayoutInflater.from(DetailActivity.this.getParent());
            
            LayoutInflater inflater = LayoutInflater.from(DetailActivity.this.getParent());
                view = inflater.inflate(R.layout.spinner_dropdown_display, null);
                TextView websiteName = (TextView) view.findViewById(R.id.spinnerDropDownItem);
            if (siteList.get(position).getName() != null) {
                websiteName.setText(siteList.get(position).getName());
                websiteName.setTextColor(0xFF000000);
            }
            return view;
        }
    }

我做错了吗?

请帮我...非常感谢

【问题讨论】:

    标签: android android-tabhost android-tabactivity android-context


    【解决方案1】:

    通过

    YourActivty.this.getParent()
    

    作为ContextSpinner

    有关详细信息,请参阅herehere

    【讨论】:

    • 我之前尝试过,但没有成功,所以我将 SpinnerListAdapter 类放在同一个类中,并且在我需要使用上下文的任何地方都没有传递任何参数我正在使用 Class.this.getParent( ) 在 SpinnerListAdapter 类中寻找 LayoutInflater
    【解决方案2】:

    如果您在选项卡活动中提供任何警报对话框,那么在这种情况下,您设置了选项卡活动的上下文而不是当前活动对象。

    而不是

    adapter = new KeywordAdapter(getApplicationContext(),id,getLNApplication().getKeyworddetail());
    listTags.setAdapter(adapter);
    

    使用下面的

    adapter = new KeywordAdapter(Tabs.ctx,id,getLNApplication().getKeyworddetail());
    listTags.setAdapter(adapter);
    

    其中 Tabs.ctx 是选项卡活动的上下文,它是静态变量。

    【讨论】:

    • 它是列表视图微调器适配器的适配器是 spinneAdapter
    • 如果你在那个时候动态创建微调器 Spinner spinner = new Spinner(Tabs.ctx);
    • 这是该活动的上下文,因为在选项卡活动中,您必须创建一个静态变量,该变量将保持该活动的引用,如公共静态选项卡 ctx; ctx = 这个;在标签活动中。
    • 公共静态标签 ctx; ctx = Tabs.this ;在这种情况下,选项卡是我的选项卡活动,我正在创建该选项卡活动的上下文。我觉得你做错了。
    • 把你的代码发邮件给我,我会在 sumant4943@gmail.com 上查看
    【解决方案3】:

    TabWidget 也可以使用此代码解决此错误

    View view = LayoutInflater.from(this.getParent()).inflate(R.layout.package_detail, null);
    this.setContentView(view); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      • 2023-03-09
      • 2013-08-26
      • 1970-01-01
      相关资源
      最近更新 更多