2010-12-30 3 views
1

사용자 지정 대화 상자를 만드는 데 문제가 있습니다. 그러나 나는 실패를 찾지 못한다. 바라건대 사람이 ...사용자 지정 대화 상자를 만드는 데 문제가 있습니다.

protected Dialog onCreateDialog(int id) { 
    Dialog dialog = null; 
    switch (id) { 
    case DIALOG_ABOUT_ID: 
     dialog = buildAboutDialog(); 
     break; 
    default: 
     dialog = null; 
    } 
    return dialog; 
} 

... 다음과 같은 오류에

public Dialog buildAboutDialog() { 
    Context mContext = getApplicationContext(); 
    Dialog dialog = new Dialog(mContext); 

    dialog.setContentView(R.layout.about_dialog); 
    dialog.setTitle("About this application"); 

    return dialog; 
} 

결과 나에게 도움이 될 수 있습니다 나는 경우 반환 된 대화 == 널 (null)을 확인

12-30 19:27:02.593: ERROR/AndroidRuntime(383): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 

를 -하지만 그렇지 않습니다.

는 또한 대화 상자가

Dialog dialog = new Dialog(this); 

하지

Context mContext = getApplicationContext(); 
Dialog dialog = new Dialog(mContext); 

I 돈으로 작성 될 필요가 있음을, 내가 발견 http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

답변

3

에서 설명하는 두 번째 방법 (인플레이터를) 시도 이유를 정확히 알지 못합니다. 아마 아무나 나에게 설명 할 수 있을까?

+3

대화 상자가 응용 프로그램이 아닌 활동에 연결되어야하기 때문에. – rf43

+0

버튼을 클릭 한 후 대화 상자를 표시하려면 어떻게해야합니까? "this"는 Activity가 아닌 OnClickListener를 참조합니다 ... – ffleandro

+0

불행히도이 사용자 정의 대화 상자를 만드는 Google 코드는 getApplicationContext가 아닙니다 ... http : //developer.android.com/guide/topics/ui /dialogs.html#CustomDialog – JPM

1

대화 상자 = 새 대화 상자 (contex); dialog.setContentView (R.layout.help_content);

이 작품은 .. getapplicationcontext 메인 클래스의 컨텍스트를 가져 오지 않을 수 있습니다.

0

활동의 컨텍스트는 getApplicationContext()에 의해 반환 된 개체와 다릅니다. 이것은 로깅을 사용하여 확인할 수 있으며 ActivityName.this 및 getApplicationContext 만 출력합니다.

getApplicationContext에 의해 리턴 된 오브젝트는 전역적인 것이지만, 활동의 컨텍스트는 해당 활동에만 속합니다.

Log.e(tag,""+ getApplicationContext()); 
Log.e(tag,""+CustomDialogActivity.this); 

여기서 CustomDialogActivity는 대화 상자를 표시하려는 내 활동입니다.

대화 상자에는 작업의 컨텍스트가 필요하며 getApplicationContext()는이를 제공하지 않습니다. 쓰여진대로 here (의견 읽기) 활동의 컨텍스트는 getApplicationContext()의 상위 집합입니다. 그래서 항상 글로벌 컨텍스트가 아닌 활동 컨텍스트를 전달하는 것이 좋습니다.

또한 onClick() 내부에있는 경우이 페이지의 ffleandro의 의견에 대답하기 위해 ActivityName을 사용하여 활동을 참조 할 수 있습니다. 희망이 있습니다

관련 문제