2012-11-29 97 views
0

當我在Activity中啓動ProgressDialog時,一切正常。 當我按下對話框上的後退按鈕以恢復Activity時,我收到一個錯誤:「無法添加窗口令牌[email protected]無效,您的活動正在運行嗎?」。 有沒有辦法關閉ProgressDialog當我想完成這個Activity,然後再次顯示它,下一次我開始這個ActivityAndroid ProgressDialog錯誤恢復活動時

我的代碼:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mFacebook = new Facebook(APP_ID); 
    mAsyncRunner = new AsyncFacebookRunner(mFacebook); 
    SessionStore.restore(mFacebook, this); 
    setContentView(R.layout.login_view); 
    mLoginButton = (LoginButton) findViewById(R.id.login); 
    SessionEvents.addAuthListener(new SampleAuthListener()); 
    mLoginButton.init(this, mFacebook); 
} 

@Override 
public void onResume() { 
    super.onResume(); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
} 

public class SampleAuthListener implements AuthListener { 

    public void onAuthSucceed() { 
     TheGaffer.this.progressDialog = ProgressDialog.show(TheGaffer.this, "Loading", "Please wait..."); 
     Bundle params = new Bundle(); 
     params.putString("fields", "name,id"); 
     mAsyncRunner.request("me", params, new LoginRequestListener()); 
    } 

    public void onAuthFail(String error) { 
     Toast.makeText(getApplicationContext(), error, 
       Toast.LENGTH_LONG).show(); 
    } 
} 

public class LoginRequestListener implements RequestListener { 

    public void onComplete(final String response, final Object state) { 
     try { 
      JSONObject jsonObjSend = new JSONObject(); 
      JSONObject json = Util.parseJson(response); 
      final String fbid = json.getString("id"); 
      jsonObjSend.put("fbid", json.getString("id")); 
      jsonObjSend.put("username", json.getString("name")); 
      jsonObjSend.put("playerPhoto", "http://graph.facebook.com/"+ json.getString("id") +"/picture"); 
      HttpClient.SendHttpPost("/user_profiles/registerUser", jsonObjSend); 
      TheGaffer.this.runOnUiThread(new Runnable() { 
       public void run() { 
        if (TheGaffer.this.progressDialog != null) { 
         TheGaffer.this.progressDialog.dismiss(); 
        } 
        Intent intent = new Intent(TheGaffer.this, TeamActivity.class); 
        intent.putExtra("fbid", fbid); 
        startActivity(intent); 
       } 
      }); 
     } catch (JSONException e) { 
      Log.w("Facebook-Example", "JSON Error in response"); 
     } catch (FacebookError e) { 
      Log.w("Facebook-Example", "Facebook Error: " + e.getMessage()); 
     } 
    } 

    public void onFacebookError(FacebookError e, final Object state) { 
     Log.e("Facebook", e.getMessage()); 
     e.printStackTrace(); 
    } 

    public void onFileNotFoundException(FileNotFoundException e, 
             final Object state) { 
     Log.e("Facebook", e.getMessage()); 
     e.printStackTrace(); 
    } 

    public void onIOException(IOException e, final Object state) { 
     Log.e("Facebook", e.getMessage()); 
     e.printStackTrace(); 
    } 

    public void onMalformedURLException(MalformedURLException e, 
             final Object state) { 
     Log.e("Facebook", e.getMessage()); 
     e.printStackTrace(); 
    } 
} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) 
{ 
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) 
    { 
     TheGaffer.this.progressDialog = null; 
     finish(); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 

}

+0

你解決了你的問題嗎? – MAC

+0

問題依然存在,我想也許是因爲我打完了() –

回答

0

你必須調用

prgDialog.dismiss();

當你想解僱它。那麼這個錯誤將不會出現

0

這是因爲進度對話框試圖將自己展示給已經關閉的Activity。 在顯示對話框,進度條之前請檢查isFinished()