Android

WindowManager: android.view.WindowLeaked: Activity com.app.test.MainActivity has leaked window DecorView@f47fdc0[MainActivity] that was originally added here 해결방법

다이얼로그 호출하다가 발생한 오류이다.

WindowManager: android.view.WindowLeaked: Activity com.app.test.MainActivity has leaked window DecorView@f47fdc0[MainActivity] that was originally added here
        at android.view.ViewRootImpl.<init>(ViewRootImpl.java:620)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:377)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)
        at android.app.Dialog.show(Dialog.java:342)
        at ddolcat.app.hi.lotto.ui.home.HomeFragment.PersonalInformationSecurity(HomeFragment.java:599)
        at ddolcat.app.hi.lotto.ui.home.HomeFragment.checkPermission(HomeFragment.java:556)
        at ddolcat.app.hi.lotto.ui.home.HomeFragment.launchActivity(HomeFragment.java:543)
        at ddolcat.app.hi.lotto.ui.home.HomeFragment$1.onClick(HomeFragment.java:205)
        at android.view.View.performClick(View.java:7259)
        at android.view.View.performClickInternal(View.java:7236)
        at android.view.View.access$3600(View.java:801)
        at android.view.View$PerformClick.run(View.java:27892)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
        Button scanQR = rootView.findViewById(R.id.scan_btn);
        
        scanQR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                launchActivity(FullScannerActivity.class);
                finish(); //문제를 유발시킨 코드 
            }
        });
        
        

   public void launchActivity(Class<?> clss) {
        mClss = clss;
        if (Build.VERSION.SDK_INT < 23) {  //마시멜로우 이전버전은 권한 방법 기존방법 적용
            Intent intent = new Intent(getActivity(), clss);
            startActivity(intent);
            getActivity().finish();
        } else {// 마시멜로 버전 이상 확인
            checkPermission();
        }
    }


    private void checkPermission() {
        try {
            if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED ) {  //권한 허용상태인지 체크

                PersonalInformationSecurity();
            } else {
                Intent intent = new Intent(getActivity(), mClss);
                startActivity(intent);
                getActivity().finish();
            }
        }catch (ActivityNotFoundException e){
            //
        }
    }
    // 개인정보 보안 법에 따른 안내 처리
    private void PersonalInformationSecurity() {
        try{
                final Dialog personDialog = new Dialog(getActivity()); 
                personDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                personDialog.setContentView(R.layout.dialog_permisson_view);
                if(personDialog.getWindow()!=null)
                    personDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));        //Android: how to create a transparent dialog-themed activity
                personDialog.setCancelable(false);
                Button bunConfirm = (Button) personDialog.findViewById(R.id.bunConfirm);
                //LinearLayout btnLayout = (LinearLayout) personDialog.findViewById(R.id.btnLayout);
                bunConfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (v.getId() == R.id.bunConfirm) {
                            if (personDialog != null && personDialog.isShowing()) {
                                personDialog.dismiss(); 

                                if (Build.VERSION.SDK_INT >= 23) {  //마시멜로우 부터
                                    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {  //권한 허용상태인지 체크
                                        // requestPermissions 을 통해 권한 요청
                                        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, ZXING_CAMERA_PERMISSION);
                                    }
                                }
                            }
                        }
                    }
                });

                if( personDialog!=null && !personDialog.isShowing()) {
                    personDialog.show();
                }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

[해결법]

다이얼로그 생성 코드에는 문제가 없다.

다이얼로그를 호출되기전에 MainActivity가 종료되었기 때문에 다이얼로그 창을 추가할 수 없는 것이다.

소스코드상에서 다이얼로그 호출하기 전에 finish()를 하고 있지 않은지 체크한다.

        Button scanQR = rootView.findViewById(R.id.scan_btn);
        
        scanQR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                launchActivity(FullScannerActivity.class);
                //finish(); //문제를 유발시킨 코드 
            }
        });
        

Leave a Reply

error: Content is protected !!