AsyncTask和HttpUrlConnection在点击时崩溃

时间:2012-03-07 21:31:00

标签: java android android-asynctask httpurlconnection

我有以下代码。当我试图调用SongSearch.execute(“searchterm”); 应用程序崩溃。我在里面调用类onClick void。我还删除了SongSearch.execute(“searchterm”);从onClick void内部,应用程序没有崩溃。问题与SongSearch类有关。这是我的代码。提前谢谢

class SongSearch extends AsyncTask<String, String, String> {


 @Override
 protected void onPreExecute() {
     super.onPreExecute();
     dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
     showDialog(DIALOG_DOWNLOAD_PROGRESS);



 }


 protected String doInBackground(String aurl) {
     int count;

     /////////////////////
     try
        {
          // create a url object
          URL url = new URL("http://google.com/search?sourceid=navclient&btnI=1&q=" + URLEncoder.encode(aurl, "UTF-8"));

          // create a urlconnection object
          URLConnection urlConnection = url.openConnection();

          urlConnection.connect();
          // wrap the urlconnection in a bufferedreader
          BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

          String line=bufferedReader.readLine();
          musicurl = urlConnection.getURL().toString();
           musicurlfinal = musicurl.replace(".com/", ".com/dl/");

          // read from the urlconnection via the bufferedreader
          while ((line = bufferedReader.readLine()) != null)
          {

          }
          bufferedReader.close();
        }
        catch(Exception e)
        {
          e.printStackTrace();
        }
        return null;






 }
 protected void onProgressUpdate(String... progress) {
      Log.d("ANDRO_ASYNC",progress[0]);
      mProgressDialog.setProgress(Integer.parseInt(progress[0]));
 }

 protected void onPostExecute(String unused) {

     dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
     startDownload();


 }


@Override
protected String doInBackground(String... arg0) {
    // TODO Auto-generated method stub
    return null;
}

}

这是我的logcat

03-07 23:43:19.124: D/dalvikvm(6752): GC freed 720 objects / 57056 bytes in 74ms
03-07 23:43:20.224: I/AirpushSDK(6752): Push Service doPush....true
03-07 23:43:20.224: I/AirpushSDK(6752): Push Service doSearch....true
03-07 23:43:20.404: I/User Agent(6752): User Agent : true
03-07 23:43:20.414: D/LocationManager(6752): Constructor: service = android.location.ILocationManager$Stub$Proxy@4590f7b0
03-07 23:43:20.514: I/AirpushSDK(6752): Initialising.....
03-07 23:43:20.674: D/dalvikvm(6752): GC freed 1442 objects / 130312 bytes in 59ms
03-07 23:43:26.527: I/AirpushSDK(6752): SDK will restart in 13260000 ms.
03-07 23:43:28.864: D/AndroidRuntime(6752): Shutting down VM
03-07 23:43:28.864: W/dalvikvm(6752): threadid=3: thread exiting with uncaught exception (group=0x40026160)
03-07 23:43:28.864: E/AndroidRuntime(6752): Uncaught handler: thread main exiting due to uncaught exception
03-07 23:43:28.864: E/AndroidRuntime(6752): java.lang.IllegalArgumentException: no dialog with id 0 was ever shown via Activity#showDialog
03-07 23:43:28.864: E/AndroidRuntime(6752):     at android.app.Activity.missingDialog(Activity.java:2524)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at android.app.Activity.dismissDialog(Activity.java:2509)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at com.freemusicdownloader.androdes.MusicDownloaderActivity$SongSearch.onPreExecute(MusicDownloaderActivity.java:403)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at android.os.AsyncTask.execute(AsyncTask.java:391)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at com.freemusicdownloader.androdes.MusicDownloaderActivity$3.onClick(MusicDownloaderActivity.java:172)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at android.view.View.performClick(View.java:2418)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at android.view.View.onTouchEvent(View.java:4233)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at android.view.View.dispatchTouchEvent(View.java:3763)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:968)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:968)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:968)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:968)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at android.app.Activity.dispatchTouchEvent(Activity.java:2064)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at android.os.Looper.loop(Looper.java:123)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at android.app.ActivityThread.main(ActivityThread.java:4370)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at java.lang.reflect.Method.invokeNative(Native Method)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at java.lang.reflect.Method.invoke(Method.java:521)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-07 23:43:28.864: E/AndroidRuntime(6752):     at dalvik.system.NativeStart.main(Native Method)
03-07 23:43:28.884: E/SemcCheckin(6752): Get crash dump level : java.io.FileNotFoundException: /data/semc-checkin/crashdump
03-07 23:43:28.884: I/dalvikvm(6752): threadid=7: reacting to signal 3
03-07 23:43:28.904: I/dalvikvm(6752): Wrote stack trace to '/data/anr/traces.txt'

1 个答案:

答案 0 :(得分:1)

在没有需要解雇的对话框的情况下调用dismissDialog(DIALOG_DOWNLOAD_PROGRESS);将抛出IllegalArgumentException(甚至在Android JavaDoc中指定了它) - 您需要保持允许您检查的状态是否需要关闭此对话框。