致命的异常AsyncTask

时间:2014-05-06 05:45:31

标签: android webview android-asynctask

在我的应用程序中使用我的webview中的超链接和shouldOverrideUrlLoading(WebView视图,字符串url)来触发异步功能...如果用户只按一次链接,它可以正常运行...但是如果用户点击了链接多次(允许用户在应用程序中多次单击链接)我的应用程序关闭“Unfortunatley,您的应用程序已停止”,我的logcat中出现以下错误:

05-05 19:28:08.920: E/AndroidRuntime(6983): FATAL EXCEPTION: AsyncTask #3
05-05 19:28:08.920: E/AndroidRuntime(6983): Process: com.soto.splash, PID: 6983
05-05 19:28:08.920: E/AndroidRuntime(6983): java.lang.RuntimeException: An error occured while executing doInBackground()
05-05 19:28:08.920: E/AndroidRuntime(6983):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
05-05 19:28:08.920: E/AndroidRuntime(6983):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
05-05 19:28:08.920: E/AndroidRuntime(6983):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
05-05 19:28:08.920: E/AndroidRuntime(6983):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
05-05 19:28:08.920: E/AndroidRuntime(6983):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-05 19:28:08.920: E/AndroidRuntime(6983):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-05 19:28:08.920: E/AndroidRuntime(6983):     at java.lang.Thread.run(Thread.java:864)
05-05 19:28:08.920: E/AndroidRuntime(6983): Caused by: java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated.
05-05 19:28:08.920: E/AndroidRuntime(6983): Make sure to release the connection before allocating another one.
05-05 19:28:08.920: E/AndroidRuntime(6983):     at ch.boye.httpclientandroidlib.impl.conn.BasicClientConnectionManager.getConnection(BasicClientConnectionManager.java:161)
05-05 19:28:08.920: E/AndroidRuntime(6983):     at ch.boye.httpclientandroidlib.impl.conn.BasicClientConnectionManager$1.getConnection(BasicClientConnectionManager.java:138)
05-05 19:28:08.920: E/AndroidRuntime(6983):     at ch.boye.httpclientandroidlib.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:455)
05-05 19:28:08.920: E/AndroidRuntime(6983):     at ch.boye.httpclientandroidlib.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:902)
05-05 19:28:08.920: E/AndroidRuntime(6983):     at ch.boye.httpclientandroidlib.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:801)
05-05 19:28:08.920: E/AndroidRuntime(6983):     at ch.boye.httpclientandroidlib.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:780)
05-05 19:28:08.920: E/AndroidRuntime(6983):     at com.soto.splash.network.NetworkOperations.addlike(NetworkOperations.java:681)
05-05 19:28:08.920: E/AndroidRuntime(6983):     at com.soto.splash.Gallery$LongOperation.doInBackground(Gallery.java:648)
05-05 19:28:08.920: E/AndroidRuntime(6983):     at     com.soto.splash.Gallery$LongOperation.doInBackground(Gallery.java:1)
05-05 19:28:08.920: E/AndroidRuntime(6983):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-05 19:28:08.920: E/AndroidRuntime(6983):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-05 19:28:08.920: E/AndroidRuntime(6983):     ... 3 more

这是我的forOverrideUrlLoading for webview:

      @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            if (url.endsWith("&photo")) 
            {
         new LongOperation().execute("");
                return true;
              }
        }

然后这是我的异步:

 private class LongOperation extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        NetworkOperations.add(url2, "", "");
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}

他们是解决这个问题的解决方案吗?我有多个Asyncs?

2 个答案:

答案 0 :(得分:0)

问题是您是从静态类一次又一次地连接到同一个函数/网络。

发生的事情是......假设您当前正在通过静态类方法执行网络操作,并再次用户单击将创建另一个网络操作的链接..现在,那时第一个网络操作连接仍在但是你想要执行另一个连接,这将导致

    connection still allocated.
05-05 19:28:08.920: E/AndroidRuntime(6983): Make sure to release the connection before allocating another one.

您需要做的是每次要进行网络操作时创建该类的新实例..并将结果传递给onPostExecute,您将在其中添加结果..

答案 1 :(得分:0)

我会做以下事情。使onProgressUpdate(....)中的按钮不闪烁,并在onPostExecute(...)

中将其设置为可回复
protected void onProgressUpdate(Integer... progress) {
   <your_button>.setClickable(false);  
}

然后在onPostExecute(...)

中将其恢复为可点击状态
protected void onPostExecute(Long result) {
   <your_button>.setClickable(true);  
}