문제 설명
대화 상자를 표시하려고 할 때 앱이 충돌함 (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 오류
612‑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 John、Pankaj、ρяσѕρєя K、bongo、SacreDeveloper、Zain)