2011-08-18 6 views
1

ProgressDialog를 실행하는 데 실제 문제가 있습니다. 내 코드 : 그 다음은Android : ProgressDialog 실패

ProgressDialog dialog; 

try { 
    dialog = new ProgressDialog(context); 
    dialog.setCancelable(true); 
    dialog.setMessage("Loading ..."); 
    // set the progress to be horizontal 
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
    // set the bar to the default value of 0 
    dialog.setProgress(0); 

    // set the maximum value 
    dialog.setMax(100); 
    // display the progressbar 
    dialog.show(); 
} 
catch (Exception e1) 
{ 
    e1.printStackTrace(); 
} 

나는 몇 가지 물건을로드하고 진행률 표시 줄을 업데이트 내 배경 스레드를 만들 수 있지만, 멀리 것을 얻을 수 없다. 스택 추적에서 "창을 추가 할 수 없습니다 - 응용 프로그램에 대한 토큰 null이 아닙니다."대화 상자가 디버거에서 모든 올바른 작업을 수행 할 것으로 보이지만 null이 아닙니다. 오류.

아무에게도 빛을 비출 수 있습니까?

답변

5

ProgressDialog를 만드는 데 어떤 종류의 컨텍스트를 사용하고 있습니까?

나는 ProgressDialog가 ApplicationContext와 함께 작동하기를 원한다고 생각한다. API가 컨스트럭터 대신 Activity를 요청해야하는 생성자가 정확하지 않습니다.

일반 Context 대신 Activity에 대한 참조를 생성자에 전달하려고합니다. 대신 사용

:

Context context = getApplicationContext(); 

사용

Context context = this; 

또는 당신이 당신의 활동 (리스너, 또는 작업)의 내부 클래스에있는 경우 사용

Context context = MyActivityNameComesHere.this; 

이에 대한 Android Bugtracker의 Issue을 참조하십시오.

+0

죄송합니다. @Janusz 저는 (멍청한 놈) 무슨 뜻인지 확실하지 않습니다. 전달하는 컨텍스트는 다음과 같습니다. Context context = getApplicationContext(); - 활동에 대한 참조를 전달하는 것이 무슨 뜻인지 잘 모르겠습니다. 무슨 뜻인지 보여주는 코드 줄 줄래? 많은 감사합니다. – Barry

+0

내 게시물을 편집 예 – Janusz

+0

+1 getApplicationContext()를 사용하면 대화 상자를 표시 할 때 FC가 발생합니다. – dymmeh