2016-05-05 137 views
3

我打電話給主要活動的另一個活動在後臺下載報價。它有時可以正常工作,但如果用戶按下按鈕並退出應用程序,則會嘗試再次打開應用程序,有時應用程序會崩潰,並顯示以下錯誤。

我已經添加了logcat的錯誤以及我從主要活動調用的類。我的代碼有什麼問題會導致應用程序崩潰?

,我從主要活動調用類

private class CheckUpdates extends AsyncTask<String, Void, Void> { 

     ProgressDialog mProgressDialog; 

     @Override 
     protected void onPostExecute(Void result) { 

      mProgressDialog.dismiss(); 
      Log.d("DESOLF", "dismiss loading dialog"); 
      if (json != null) { 
       if (jsonResultNull.equals("true")) { 

        AlertDialog.Builder builder = new AlertDialog.Builder(
          context); 
        builder.setTitle("Check Updates"); 
        builder.setMessage("There are not any updates!"); 

        builder.setNeutralButton("OK", 
          new DialogInterface.OnClickListener() { 

           public void onClick(DialogInterface dialog, 
             int id) { 

           } 

          }); 
        if(SettingsActivity.tocheck==1){ 
        builder.show(); 
        } 

       } else if(SettingsActivity.tocheck==1) { 
        AlertDialog.Builder builder = new AlertDialog.Builder(
          context); 
        builder.setTitle("New Data Available"); 
        String messageTxt = ""; 
        if (authors.length() != 0 && quotes.length() != 0) { 
         messageTxt = String.valueOf(authors.length()) 
           + " Categories and " 
           + String.valueOf(quotes.length()) + " Quotes"; 
        } else if (authors.length() != 0 && quotes.length() == 0) { 
         messageTxt = String.valueOf(authors.length()) 
           + " Categories"; 
        } else if (authors.length() == 0 && quotes.length() != 0) { 
         messageTxt = String.valueOf(quotes.length()) 
           + " Status"; 
        } 
        builder.setMessage("There are new " + messageTxt 
          + " Arrived. Download?"); 

        builder.setPositiveButton("Yes", 
          new DialogInterface.OnClickListener() { 

           public void onClick(DialogInterface dialog, 
             int id) { 




            Log.d("DESOLF", "start updates service"); 
            Intent getUpdates = new Intent(context, 
              GetUpdatesService.class); 
            jsonString = json.toString(); 
            //getUpdates.putExtra("json", json.toString()); 
            context.startService(getUpdates); 


           } 


          }); 


        builder.setNegativeButton("No", 
          new DialogInterface.OnClickListener() { 

           public void onClick(DialogInterface dialog, 
             int id) { 

           } 

          }); 

        builder.show(); 
       } 
       else if(SettingsActivity.tocheck==0) 
       { 
        Log.d("DESOLF", "start updates service"); 
        Intent getUpdates = new Intent(context, 
          GetUpdatesService.class); 
        jsonString = json.toString(); 
        //getUpdates.putExtra("json", json.toString()); 
        context.startService(getUpdates); 
       } 
      } else { 
       AlertDialog.Builder builder = new AlertDialog.Builder(context); 
       builder.setTitle("Internet Connection Error"); 
       builder.setMessage("Please connect to an internet connection!"); 

       builder.setNeutralButton("OK", 
         new DialogInterface.OnClickListener() { 

          public void onClick(DialogInterface dialog, int id) { 

          } 

         }); 
       builder.show(); 
      } 

     } 

     // ------------------------------------------------------------------------ 

     @Override 
     protected void onPreExecute() { 
      Log.d("DESOLF", "show loading dialog"); 
      mProgressDialog = ProgressDialog.show(context, "Please Wait", 
        "Downloading New Status..."); 
      mProgressDialog.setOnKeyListener(new OnKeyListener() { 
       @Override 
       public boolean onKey(DialogInterface dialog, int keyCode, 
         KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_BACK 
          && event.getAction() == KeyEvent.ACTION_UP) 
         mProgressDialog.dismiss(); 
        return false; 
       } 
      });   
     } 

我的錯誤的logcat:

05-05 11:03:33.856: E/AndroidRuntime(19379): FATAL EXCEPTION: main 
05-05 11:03:33.856: E/AndroidRuntime(19379): Process: com.example.app, PID: 19379 
05-05 11:03:33.856: E/AndroidRuntime(19379): android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running? 
05-05 11:03:33.856: E/AndroidRuntime(19379): at android.view.ViewRootImpl.setView(ViewRootImpl.java:574) 
05-05 11:03:33.856: E/AndroidRuntime(19379): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:282) 
05-05 11:03:33.856: E/AndroidRuntime(19379): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85) 
05-05 11:03:33.856: E/AndroidRuntime(19379): at android.app.Dialog.show(Dialog.java:298) 
05-05 11:03:33.856: E/AndroidRuntime(19379): at android.app.AlertDialog$Builder.show(AlertDialog.java:993) 
05-05 11:03:33.856: E/AndroidRuntime(19379): at com.example.app.UpdateClass$CheckUpdates.onPostExecute(UpdateClass.java:207) 
05-05 11:03:33.856: E/AndroidRuntime(19379): at com.example.app.UpdateClass$CheckUpdates.onPostExecute(UpdateClass.java:1) 
05-05 11:03:33.856: E/AndroidRuntime(19379): at android.os.AsyncTask.finish(AsyncTask.java:636) 
05-05 11:03:33.856: E/AndroidRuntime(19379): at android.os.AsyncTask.access$500(AsyncTask.java:177) 
05-05 11:03:33.856: E/AndroidRuntime(19379): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653) 
05-05 11:03:33.856: E/AndroidRuntime(19379): at android.os.Handler.dispatchMessage(Handler.java:102) 
05-05 11:03:33.856: E/AndroidRuntime(19379): at android.os.Looper.loop(Looper.java:135) 
05-05 11:03:33.856: E/AndroidRuntime(19379): at android.app.ActivityThread.main(ActivityThread.java:5930) 
05-05 11:03:33.856: E/AndroidRuntime(19379): at java.lang.reflect.Method.invoke(Native Method) 
05-05 11:03:33.856: E/AndroidRuntime(19379): at java.lang.reflect.Method.invoke(Method.java:372) 
05-05 11:03:33.856: E/AndroidRuntime(19379): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
05-05 11:03:33.856: E/AndroidRuntime(19379): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 

全班

package com.example.app; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.app.AlertDialog; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnKeyListener; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.AsyncTask; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.widget.Button; 
import android.widget.Toast; 

import com.example.app.R; 

public class UpdateClass { 

    public Context context; 

    Button zain; 

    String siteUrl, updatesUrl; 

    DAO db; 
    Cursor c; 

    String auPictureDir; 

    int lastAuthor, lastQuote; 

    JSONArray authors = null; 
    JSONArray quotes = null; 

    JSONObject json; 
    static String jsonString; 
    String jsonResultNull = ""; 

    private ConnectionDetector cd; 
    Boolean isSDPresent = android.os.Environment.getExternalStorageState() 
      .equals(android.os.Environment.MEDIA_MOUNTED); 

    // ============================================================================== 

    public UpdateClass(Context context) { 
     this.context = context; 

     db = new DAO(context); 
     db.open(); 

     lastAuthor = db.getLastAuthor(); 
     lastQuote = db.getLastQuote(); 

     siteUrl = context.getResources().getString(R.string.siteUrl); 
     updatesUrl = siteUrl + "site/get_updates/" + String.valueOf(lastAuthor) 
       + "/" + String.valueOf(lastQuote); 

     auPictureDir = siteUrl + "global/uploads/levels/"; 

    } 

    // ============================================================================== 

    public void handleUpdates() { 

     // check first for internet 
     cd = new ConnectionDetector(context); 

     if (!cd.isConnectingToInternet()) { 
      // Internet Connection is not present 

      AlertDialog.Builder builder = new AlertDialog.Builder(context); 
      builder.setTitle("Internet Connection Error"); 
      builder.setMessage("Please connect to an internet connection!"); 

      builder.setNeutralButton("OK", 
        new DialogInterface.OnClickListener() { 

         public void onClick(DialogInterface dialog, int id) { 

         } 

        }); 

      builder.show(); 

     } else { 
      new CheckUpdates().execute(new String[] { updatesUrl }); 
     } 

    } 

    public static String getJsonString(){ 
     return jsonString; 
    } 

    private class CheckUpdates extends AsyncTask<String, Void, Void> { 

     ProgressDialog mProgressDialog; 

     @Override 
     protected void onPostExecute(Void result) { 

      mProgressDialog.dismiss(); 
      Log.d("DESOLF", "dismiss loading dialog"); 
      if (json != null) { 
       if (jsonResultNull.equals("true")) { 

        AlertDialog.Builder builder = new AlertDialog.Builder(
          context); 
        builder.setTitle("Check Updates"); 
        builder.setMessage("There are not any updates!"); 

        builder.setNeutralButton("OK", 
          new DialogInterface.OnClickListener() { 

           public void onClick(DialogInterface dialog, 
             int id) { 

           } 

          }); 
        if(SettingsActivity.tocheck==1){ 
        builder.show(); 
        } 

       } else if(SettingsActivity.tocheck==1) { 
        AlertDialog.Builder builder = new AlertDialog.Builder(
          context); 
        builder.setTitle("New Data Available"); 
        String messageTxt = ""; 
        if (authors.length() != 0 && quotes.length() != 0) { 
         messageTxt = String.valueOf(authors.length()) 
           + " Categories and " 
           + String.valueOf(quotes.length()) + " Quotes"; 
        } else if (authors.length() != 0 && quotes.length() == 0) { 
         messageTxt = String.valueOf(authors.length()) 
           + " Categories"; 
        } else if (authors.length() == 0 && quotes.length() != 0) { 
         messageTxt = String.valueOf(quotes.length()) 
           + " Status"; 
        } 
        builder.setMessage("There are new " + messageTxt 
          + " Arrived. Download?"); 

        builder.setPositiveButton("Yes", 
          new DialogInterface.OnClickListener() { 

           public void onClick(DialogInterface dialog, 
             int id) { 




            Log.d("DESOLF", "start updates service"); 
            Intent getUpdates = new Intent(context, 
              GetUpdatesService.class); 
            jsonString = json.toString(); 
            //getUpdates.putExtra("json", json.toString()); 
            context.startService(getUpdates); 


           } 


          }); 


        builder.setNegativeButton("No", 
          new DialogInterface.OnClickListener() { 

           public void onClick(DialogInterface dialog, 
             int id) { 

           } 

          }); 

        builder.show(); 

       } 
       else if(SettingsActivity.tocheck==0) 
       { 
        Log.d("DESOLF", "start updates service"); 
        Intent getUpdates = new Intent(context, 
          GetUpdatesService.class); 
        jsonString = json.toString(); 
        //getUpdates.putExtra("json", json.toString()); 
        context.startService(getUpdates); 
       } 
      } else { 
       AlertDialog.Builder builder = new AlertDialog.Builder(context); 
       builder.setTitle("Internet Connection Error"); 
       builder.setMessage("Please connect to an internet connection!"); 

       builder.setNeutralButton("OK", 
         new DialogInterface.OnClickListener() { 

          public void onClick(DialogInterface dialog, int id) { 

          } 

         }); 
       if(!CheckUpdates.isFinishing()){ 
       builder.show(); 
       } 

      } 

     } 

     // ------------------------------------------------------------------------ 

     @Override 
     protected void onPreExecute() { 
      Log.d("DESOLF", "show loading dialog"); 
      mProgressDialog = ProgressDialog.show(context, "Please Wait", 
        "Downloading New Status..."); 
      mProgressDialog.setOnKeyListener(new OnKeyListener() { 
       @Override 
       public boolean onKey(DialogInterface dialog, int keyCode, 
         KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_BACK 
          && event.getAction() == KeyEvent.ACTION_UP) 
         mProgressDialog.dismiss(); 
        return false; 
       } 
      });   
     } 

     // ------------------------------------------------------------------------ 

     @Override 
     protected Void doInBackground(String... params) { 

      // Creating JSON Parser instance 
      JSONParser jParser = new JSONParser(); 

      // getting JSON string from URL 
      // Log.e("url", params[0]); 
      Log.d("DESOLF", "url : params[0]"); 
      json = jParser.getJSONFromUrl(params[0]); 
      Log.d("DESOLF", "json string has been downloaded"); 

      try { 
       if (json != null) { 
        authors = json.getJSONArray("authors"); 
        quotes = json.getJSONArray("quotes"); 

        // Log.e("quotes", String.valueOf(quotes.length())); 
        if (authors.length() == 0 && quotes.length() == 0) { 
         jsonResultNull = "true"; 
        } 
       } else { 
        jsonResultNull = "true"; 
       } 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      return null; 
     } 
    } 
} 

回答

2

無論您在何處添加builder.show(),請將活動是否完成的條件。

您的上下文引用更改到活動中UpdateClass構造:

public UpdateClass(Activity context) { 
    //this.context also should be Activity, so please make it as a Activity Reference 
    this.context = context; 

    db = new DAO(context); 
    db.open(); 

    lastAuthor = db.getLastAuthor(); 
    lastQuote = db.getLastQuote(); 

    siteUrl = context.getResources().getString(R.string.siteUrl); 
    updatesUrl = siteUrl + "site/get_updates/" + String.valueOf(lastAuthor) 
      + "/" + String.valueOf(lastQuote); 

    auPictureDir = siteUrl + "global/uploads/levels/"; 

} 

像:

if(!context.isFinishing()){ //here activity means your activity class 

    builder.show(); 
} 
+0

喜!對不起,我在開發和學習方面並不新鮮。你能告訴我在上面我的代碼,這是我應該用在activity.isFinishing()中的活動嗎? –

+0

您的asyncTask是否在活動類中聲明.. ?? –

+0

你在哪裏獲得了asyncTask中的上下文引用? –

0

複製MainActivity類別裏面全CheckUpdates類使用MainActivity.this而不是CheckUpdates類中的上下文。

相關問題