2014-06-16 3 views
0

AsyncTask 클래스를 호출하면 My ProgressDialog이 충돌합니다. AsyncTask 오류 다음 발생 :AsyncTask 클래스에서 progressdialog 오류가 발생합니다.

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 

이 내가 전화하는 방법입니다 내 AsyncTask 클래스

new CopyToSDHelper(Environment.getExternalStorageDirectory() 
      + File.separator + fileName, context, fileName, numberOfContact).execute(); 

그리고 이것은 내 AsyncTask를 클래스

public class CopyToSDHelper extends AsyncTask<Object, Integer, Void> 
{ 
    // Notification Manager 
    private NotificationManager myNotificationManager; 
    // Current API version 
    private int     currentApiVersion; 

    String      fileName  = ""; 
    String      backUpFileName = ""; 
    String      numberOfContact = ""; 
    Context      context; 
    ProgressDialog    dialog; 

    @Override 
    protected void onPreExecute() 
    { 
     dialog = new ProgressDialog(context); 
     dialog.setMessage("Uploading..."); 
     dialog.setIndeterminate(false); 
     dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     dialog.setProgress(0); 
     dialog.show(); 
     dialog.setContentView(R.layout.activity_background_submit_dialog); 
    } 

    public CopyToSDHelper(String fileName, Context context, 
      String backUpFileName, String numberOfContact) 
    { 
     this.fileName = fileName; 
     this.backUpFileName = backUpFileName; 
     this.numberOfContact = numberOfContact; 
     this.context = context; 

     this.currentApiVersion = android.os.Build.VERSION.SDK_INT; 
    } 


    @Override 
    protected Void doInBackground(Object... params) 
    { 
     // Call Notification method 
     displayNotification(); 

     WebDavWriter writer = new WebDavWriter(context, fileName); 
     try 
     { 
      int progress = Integer.parseInt(numberOfContact)/100; 

      publishProgress(progress); 
      writer.sendFileToKiki(backUpFileName); 
      Thread.sleep(2000); 

      // Now delete local file from Android 
      File file = new File(fileName); 
      file.delete(); 
     } 
     catch (Exception e) 
     { 
      new ErrLog(e.toString()); 
     } 

     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... progress) 
    { 
     dialog.setProgress(progress[0]); 
    } 

    @Override 
    protected void onPostExecute(Void result) 
    { 
     try 
     { 
      dialog.dismiss(); 
     } 
     catch (Exception e) 
     { 
     } 
    } 
} 

입니다 클래스 Activity에서 그리고이 로그 캣 결과

입니다
E/AndroidRuntime(29186): FATAL EXCEPTION: main 
E/AndroidRuntime(29186): Process: example.sample.android.SDcontactlist, PID: 29186 
E/AndroidRuntime(29186): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
E/AndroidRuntime(29186): at android.view.ViewRootImpl.setView(ViewRootImpl.java:540) 
E/AndroidRuntime(29186): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259) 
E/AndroidRuntime(29186): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 
E/AndroidRuntime(29186): at android.app.Dialog.show(Dialog.java:286) 
E/AndroidRuntime(29186): at write_helper.CopyToSDHelper.onPreExecute(CopyToSDHelper.java:43) 
E/AndroidRuntime(29186): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587) 
E/AndroidRuntime(29186): at android.os.AsyncTask.execute(AsyncTask.java:535) 
E/AndroidRuntime(29186): at utils.SaveAllEntry.vcfSection(SaveAllEntry.java:281) 
E/AndroidRuntime(29186): at utils.SaveAllEntry.access$10(SaveAllEntry.java:246) 
E/AndroidRuntime(29186): at utils.SaveAllEntry$4.onClick(SaveAllEntry.java:201) 
E/AndroidRuntime(29186): at android.view.View.performClick(View.java:4438) 
E/AndroidRuntime(29186): at android.view.View$PerformClick.run(View.java:18422) 
E/AndroidRuntime(29186): at android.os.Handler.handleCallback(Handler.java:733) 
E/AndroidRuntime(29186): at android.os.Handler.dispatchMessage(Handler.java:95) 
E/AndroidRuntime(29186): at android.os.Looper.loop(Looper.java:136) 
E/AndroidRuntime(29186): at android.app.ActivityThread.main(ActivityThread.java:5017) 
E/AndroidRuntime(29186): at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime(29186): at java.lang.reflect.Method.invoke(Method.java:515) 
E/AndroidRuntime(29186): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
E/AndroidRuntime(29186): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
E/AndroidRuntime(29186): at dalvik.system.NativeStart.main(Native Method) 

도움 주셔서 감사합니다.

안부

+0

귀하의 activity_background_submit_dialog 게시 – kgandroid

답변

0

난 당신의 코드를 실행하고 당신이 ContextgetApplicationContext() 대신의 Activity 상황 등을 이용되는 결론에 도달했다.

ContextToast.makeText()으로 전달됩니다.

+0

친애하는 Sufian. 답변 감사합니다. 불행히도 다시 충돌을 제안하고이 메시지를 throw하는대로 변경했습니다. android.util.AndroidRuntimeException : 내용을 추가하기 전에 requestFeature()를 호출해야합니다. –

+0

@yener 응답을 업데이트했습니다. – Sufian

+0

당신의 대답은 저에게 새로운 시각을 주었고 저는 문제를 해결했습니다. 해결책은 간단했습니다. getApplicationContext() 대신 ClassName.this를 사용했습니다. 희망이 다른 사람이 도움이됩니다. –

관련 문제