Android reports an error token null is not valid is your activity running in the pop-up window of the adapter

// Brief code in the Adapter class

 Context mContext;//获取环境上下文
//设置领用日期
        holder.tvDate.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    

                Toast.makeText(mContext.getApplicationContext(), "预约时间无效,请重新确认", Toast.LENGTH_SHORT).show();
                    new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
    
    
                        @Override
                        public void onDateSet(DatePicker view, int years, int months, int days) {
    
    

                            String strMonth = month < 10 ? "0" + month :"" + month;
                            String strDay = day < 10 ? "0" + day : "" + day;
                            String theDate = String.format(year + "-" + strMonth + "-" + strDay);
                            holder.tvDate.setText(theDate);
                            Toast.makeText(mContext, "选择时间", Toast.LENGTH_SHORT).show();
                        }
                    }, 2020, 7, 15).show();

                }
        });

//Activity brief code

    //点击新增领用
    @OnClick(R.id.btn_addCollection_collectionSonPage)
    void addCollectionDataView() {
    
    
        mEntryList.add(new CollectSonGoodsAddEntry());
        Toast.makeText(getBaseContext(), "新增领用", Toast.LENGTH_SHORT).show();
       
  //错误 ,getContext()依附了适配器的Context/  mCollectionSonAdapter = new CollectionSonAdapter(getContext(), mEntryList);
//修正  
  mCollectionSonAdapter = new CollectionSonAdapter(this, mEntryList)
        mRecyCollectionData.setAdapter(mCollectionSonAdapter);

        mCollectionSonAdapter.notifyDataSetChanged();
    }

It is because there is an activity that is attached to another activity. When the attached activity produces an error, the activity has no backing and an error occurs (or an activity that has been finished() is called). Generally, it is easy to see when creating alertdialog.builder.

There is no fundamental solution to this error. The only way is to change the fact that the activity is attached to another activity and put it into another activity.

Android利用ActivityGroup加载子Activity,子Activity调用对话框,弹出:
ERROR/AndroidRuntime(10104): Caused by: android.view.WindowManager B a d T o k e n E x c e p t i o n : U n a b l e t o a d d w i n d o w − − t o k e n a n d r o i d . a p p . L o c a l A c t i v i t y M a n a g e r BadTokenException: Unable to add window -- token android.app.LocalActivityManager BadTokenException:Unabletoaddwindowtokenandroid.app.LocalActivityManagerLocalActivityRecord@45a58ee0 is not valid; is your activity running?

Cause Analysis:

Because in the new dialog box, the parameter content is specified as this, which points to the content of the current child Activity. However, the child activity is dynamically created, and there is no guarantee that it will always exist. The content of its parent Activity is stable, so there are the following solutions.

Solution:

1. Replace content with getParent().
2. There is also the getContext() in the activity class, replaced with this, the Adapter is not attached to another activity, this represents the context in the current class

Guess you like

Origin blog.csdn.net/ShiXinXin_Harbour/article/details/108639330