대화 상자를 표시하려고 할 때 앱이 충돌함 (App crashed when try to display dialog box)


문제 설명

대화 상자를 표시하려고 할 때 앱이 충돌함 (App crashed when try to display dialog box)

단순히, 앱은 버튼을 클릭할 때 대화 상자를 표시해야 합니다. 그러나 클릭하면 충돌합니다. 그리고 logCat 오류가 말하려는 것이 무엇인지 이해하지 못합니다.

 addImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final Dialog dialog = new Dialog(getApplication());
                dialog.setContentView(R.layout.custom_dialog_box);
                dialog.setTitle("Alert Dialog View");
                Button btnExit = (Button) dialog.findViewById(R.id.btnExit);
                btnExit.setOnClickListener(new View.OnClickListener() {
                    @Override public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                dialog.findViewById(R.id.btnChoosePath)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override public void onClick(View v) {
                               // activeGallery();
                            }
                        });
                dialog.findViewById(R.id.btnTakePhoto)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override public void onClick(View v) {
                                //activeTakePhoto();
                            }
                        });

                // show dialog on screen
                dialog.show();
            }

            });

xml

<?xml version="1.0" encoding="utf‑8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:background="@color/light_gray"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:layout_height="fill_parent">

    <Button
        android:onClick="btnChoosePathClicked"
        android:id="@+id/btnChoosePath"
        android:background="@color/honey_dew2"
        android:textColor="@color/black"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:width="200dp"
        android:text="From Gallery"/>

    <Button
        android:onClick="btnTakePhotoClicked"
        android:id="@+id/btnTakePhoto"
        android:background="@color/honey_dew2"
        android:textColor="@color/black"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/btnChoosePath"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:width="200dp"
        android:text="Take Photo"/>

    <Button
        android:onClick="btnExitClicked"
        android:id="@+id/btnExit"
        android:background="@color/honey_dew2"
        android:textColor="@color/black"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/btnTakePhoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="200dp"
        android:text="Exit"/>
</RelativeLayout>

LogCat 오류

6
12‑09 17:57:34.344  18804‑18804/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.view.WindowManager$BadTokenException: Unable to add window ‑‑ token null is not for an application
            at android.view.ViewRootImpl.setView(ViewRootImpl.java:698)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.app.Dialog.show(Dialog.java:281)
            at com.example.project.myapplication.GUI.AddMoreClaims$1.onClick(AddMoreClaims.java:68)
            at android.view.View.performClick(View.java:4230)
            at android.view.View$PerformClick.run(View.java:17660)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)

여기서 (AddMoreClaims.java: 68)dialog.show();를 참조하십시오. 여기서 무슨 문제가 있습니까?


참조 솔루션

방법 1:

Make your dialog declaration like below code:

For Activity:

final Dialog dialog = new Dialog(Your_Activity_Name.this);

For Fragment:

final Dialog dialog = new Dialog(getActivity());

방법 2:

Unable to add window ‑‑ token null is not for an application

Because getApplication() retuning null.

Use v.getContext() or ActivityName.this to create Dialog object:

final Dialog dialog = new Dialog(v.getContext());
OR
final Dialog dialog = new Dialog(ActivityName.this);

방법 3:

You can't create Dialog with an application Context, you should use Activity Context.

new Dialog(MyActivity.this); //if you use Activity
new Dialog(getActivity()); //if you use Fragment 

방법 4:

You should show your dialog inside activity and never in application. Open your dialog in your current open activity and it should work for you.

방법 5:

As others have stated above your Context i.e. getApplication() is wrong.

This is because you need to have the current Activity's context to display anything (popup, view, dialog) over it.

Whereas you could have a getApplicationContext() to start a new Activity, you will have to use this (inside an Activity) or getActivity() inside a Fragment.

Here's more info on Context and which one to use and when

(by JohnPankajρяσѕρєя KbongoSacreDeveloperZain)

참조 문서

  1. App crashed when try to display dialog box (CC BY‑SA 2.5/3.0/4.0)

#crash #dialog #Android #java






관련 질문

WebView가 전체 활동을 죽이고 있습니다 --- 이것을 어떻게 디버깅 할 수 있습니까? (WebView killing the whole activity --- How can I debug this?)

연결 시도 시 Windows의 MySQL이 충돌함 (MySQL on Windows crashes on connection attempts)

휘발성 멤버는 멤버 함수에서 액세스할 수 없습니다. (volatile member can not be accessed by member function)

Ruby에서 Gosu로 텍스트를 인쇄할 수 없음(충돌) (Cannot print a text with Gosu in Ruby (crash))

이 기능은 .exe를 충돌시킵니다. 제가 뭘 잘못하고 있습니까? (This function makes the .exe crash, what am I doing wrong?)

PyAudio로 웨이브를 재생하는 Tkinter 버튼 호출 기능 - 충돌 (Tkinter button calling function to play wave with PyAudio -- crashes)

phonegap 카메라가 앱 충돌 (phonegap camera crashes the app)

대화 상자를 표시하려고 할 때 앱이 충돌함 (App crashed when try to display dialog box)

kaldi 설치 시 libmkl_tbb_thread.so sth 관련 문제 (A problem related to libmkl_tbb_thread.so sth when installing kaldi)

WordPress에서 Simple Jquery Click이 작동하지 않음 (Simple Jquery Click not working in WordPress)

새 줄이 발견되지 않으면 fget이 NULL을 반환하지 않습니다. (fgets dont return NULL when no new line found)

webview가 검은 색으로 바뀌고 응용 프로그램이 충돌합니다. (webview turns black and application crashes)







코멘트