마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

동적 spinner 추가 시 BadTokenException문제 입니다.

0 추천

동적으로 spniier를 추가했는데 spinner를 선택하면 어플리케이션이 죽어버리네요

context문제 같은데 어디를 고쳐야 할 지 모르겠습니다ㅜㅜ

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test__smsanalysis);
        checkSMSConent();
        DBHelper dbHelper = new DBHelper(this);
        context = this;
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList = dbHelper.selectDistintByCompany();
        ImageView imageView;
        for (int i = 0; i < arrayList.size(); i++) {
            if (arrayList.get(i).equals("농협카드")) {
                imageView = (ImageView) this.findViewById(R.id.imageView);
                imageView.setVisibility(View.VISIBLE);
            } else if (arrayList.get(i).equals("신한카드")) {
                imageView = (ImageView) this.findViewById(R.id.imageView2);
                imageView.setVisibility(View.VISIBLE);
            } else {

            }
        }


        ///
        final ImageButton btnOpenPopup = (ImageButton) findViewById(R.id.imageButton);
        btnOpenPopup.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(final View arg0) {
                LayoutInflater layoutInflater
                        = (LayoutInflater) getBaseContext()
                        .getSystemService(LAYOUT_INFLATER_SERVICE);
                final View popupView = layoutInflater.inflate(R.layout.popup, null);
                final PopupWindow popupWindow = new PopupWindow(
                        popupView,
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
                popupWindow.setFocusable(true);
                edittext = (EditText) popupWindow.getContentView().findViewById(editText);
                edittext.isLongClickable();

                Button findButton = (Button) popupView.findViewById(R.id.smsButton);
                final LinearLayout topLL = (LinearLayout)popupView.findViewById(R.id.smsLayout);
                findButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(final View view) {
                        String inPutText = edittext.getText().toString();
                        //textView.setText(checkSpecialSMSConent(inPutText)[1]);
                        ////
                        int temp = checkSpecialSMSConent(inPutText).length;
                        TextView[] topTV1 = new TextView[temp];
                        LinearLayout[] linearLayouts = new LinearLayout[temp];
                        Spinner[] spinners = new Spinner[temp];
                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
                                android.R.layout.simple_spinner_item, item);
                        for(int i = 0;i < checkSpecialSMSConent(inPutText).length;i++ ){
                            //////

                            linearLayouts[i] = new LinearLayout(view.getContext());
                            spinners[i] = new Spinner(view.getContext());
                            linearLayouts[i].setOrientation(LinearLayout.HORIZONTAL);

                            spinners[i].setAdapter(adapter);
                            //////
                            topTV1[i] = new TextView(view.getContext());
                            topTV1[i].setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                            topTV1[i].setBackgroundColor(Color.parseColor("#00FFFFFF"));
                            topTV1[i].setPadding(20, 10, 10, 10);
                            topTV1[i].setTextColor(Color.parseColor("#FF7200"));
                            topTV1[i].setTextSize(13);
                            topTV1[i].setText(checkSpecialSMSConent(inPutText)[i]);
                            linearLayouts[i].addView(spinners[i]);
                            linearLayouts[i].addView( topTV1[i]);

                            topLL.addView(linearLayouts[i]);

                        }

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.carmoneybook.myapplication, PID: 11429
                  android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@42fc4988 is not valid; is your activity running?
                      at android.view.ViewRootImpl.setView(ViewRootImpl.java:780)
                      at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:278)
                      at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
                      at android.widget.PopupWindow.invokePopup(PopupWindow.java:1087)
                      at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:974)
                      at android.widget.ListPopupWindow.show(ListPopupWindow.java:635)
                      at android.widget.Spinner$DropdownPopup.show(Spinner.java:1176)
                      at android.widget.Spinner.performClick(Spinner.java:685)
                      at android.view.View$PerformClick.run(View.java:19416)
                      at android.os.Handler.handleCallback(Handler.java:733)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:146)
                      at android.app.ActivityThread.main(ActivityThread.java:5635)
                      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:1291)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
                      at dalvik.system.NativeStart.main(Native Method)

치도리이 (200 포인트) 님이 2016년 12월 4일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...