2017-04-07 88 views
0

我在添加自動完成編輯字段時單擊浮動操作按鈕,當用戶單擊浮動操作按鈕時,在彈出窗口中添加彈出窗口彈出窗口顯示成功,但當我點擊編輯字段時,它給出了一個錯誤。 錯誤是

這裏是完整的錯誤。

Process: com.example.mubtadanaqvi.stunexus, PID: 7252 
android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running? 
at android.view.ViewRootImpl.setView(ViewRootImpl.java:769) 
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:278) 
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1067) 
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:966) 
at android.widget.ListPopupWindow.show(ListPopupWindow.java:635) 
at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1119) 
at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:973)                      at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:955) 
at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:146) 
at android.app.ActivityThread.main(ActivityThread.java:548) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
at dalvik.system.NativeStart.main(Native Method) 

這裏是我的代碼

floatingActionButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 

        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(LAYOUT_INFLATER_SERVICE); 
        final View customView = inflater.inflate(R.layout.add_new_skill, null); 
        mPopupWindow = new PopupWindow(
          customView, 
          ViewGroup.LayoutParams.MATCH_PARENT, 
          ViewGroup.LayoutParams.WRAP_CONTENT, 
          true 
        ); 
        ImageButton cancel = (ImageButton) customView.findViewById(R.id.close_button); 
        cancel.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View view) { 
          mPopupWindow.dismiss(); 
         } 
        }); 

        skillListAdapter = new ArrayAdapter<>(getContext(),android.R.layout.simple_dropdown_item_1line, skillsList); 
        skillInputField = (AutoCompleteTextView) customView.findViewById(R.id.skill_input); 
        skillInputField.setThreshold(1); 
        skillInputField.setAdapter(skillListAdapter); 
        skillListAdapter.addAll(skillsList); 
        skillListAdapter.notifyDataSetChanged(); 
        Button saveButton = (Button) customView.findViewById(R.id.save_skill); 
        saveButton.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View view) { 
          String skillText = skillInputField.getText().toString(); 
          if (skillText.isEmpty()) 
           makeToast("EMPTY"); 
          else { 
           if (ConnectivityListener.isNetwork(getContext())) { 
//         progressDialog.show(); 
            mPopupWindow.dismiss(); 
            makeToast(skillText); 
            list.add(new Skill_Item(skillText, 0)); 
            adapter.notifyDataSetChanged(); 
           } else 
            makeToast("connection error!"); 
          } 
         } 
        }); 

        mPopupWindow.showAtLocation(rootLayout, Gravity.CENTER, 0, 0); 



       } 
      }); 
+0

使用'this'代替'的getContext() ' –

+0

@JohnJoe這將不起作用,因爲ArrayAdapter是在setOnClickListner()中創建的,所以這不會返回Activity的引用。 –

+0

使用'getActivity()'而不是'getContext()' – sodhankit

回答

0

原因的異常: 活動已經結束,但你想用成品活動的背景下,以顯示一個對話框。由於沒有窗口顯示android運行時會拋出此異常。

解例外的: 請使用isFinishing()方法,其由Android調用以校驗該活動是否是在完成的過程:

private final WeakReference<MainActivity> mainActivityWeakRef; 
mainActivityWeakRef =new WeakReference<MainActivity>(this); 
if (mainActivityWeakRef.get() != null && !mainActivityWeakRef.get().isFinishing()) { 
// Your Code 
} 
相關問題