执行doInBackground()时发生android错误

时间:2012-05-30 00:03:19

标签: android

我在执行doInBackground()'

时遇到错误'错误

这是我的ASyncTask类:

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

    @Override
    protected String doInBackground(String... params) {
        String pollResult = "";
        if(((ChatApplication) _instance.getApplication()).isPolling() == false){
        ((ChatApplication) _instance.getApplication()).setPolling(true);
        pollResult = ((ChatApplication) _instance.getApplication()).GetConnectionManager().PollServer();
    }
        return pollResult;
        }

    @Override
    protected void onPostExecute(String result) {
        if(result != ""){
            HandlePollResult(result);
            ((ChatApplication) _instance.getApplication()).setPolling(false);
        }

    }

}

我的TimerTask称之为:

private Handler handler = new Handler();
public class ServerPollTimer extends TimerTask {
    private Runnable runnable = new Runnable() {
        public void run() {
            ExecutePollServer();
        }
    };

    public void run() {
        handler.post(runnable);
        }
    }

我的ExecutePollServer方法:

private void ExecutePollServer(){
    ASyncPollServer task = new ASyncPollServer();
    task.execute();
}

我在OnCreate()中调用计时器:

new Timer().schedule(new ServerPollTimer() , 0, 5000);

这是我的stacktrace:

05-30 09:46:16.020: E/AndroidRuntime(340): FATAL EXCEPTION: AsyncTask #1
05-30 09:46:16.020: E/AndroidRuntime(340): java.lang.RuntimeException: An error occured while executing doInBackground()
05-30 09:46:16.020: E/AndroidRuntime(340):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
05-30 09:46:16.020: E/AndroidRuntime(340):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
05-30 09:46:16.020: E/AndroidRuntime(340):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
05-30 09:46:16.020: E/AndroidRuntime(340):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
05-30 09:46:16.020: E/AndroidRuntime(340):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
05-30 09:46:16.020: E/AndroidRuntime(340):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
05-30 09:46:16.020: E/AndroidRuntime(340):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
05-30 09:46:16.020: E/AndroidRuntime(340):  at java.lang.Thread.run(Thread.java:1019)
05-30 09:46:16.020: E/AndroidRuntime(340): Caused by: java.lang.IllegalStateException: No wrapped connection.
05-30 09:46:16.020: E/AndroidRuntime(340):  at org.apache.http.impl.conn.AbstractClientConnAdapter.assertValid(AbstractClientConnAdapter.java:163)
05-30 09:46:16.020: E/AndroidRuntime(340):  at org.apache.http.impl.conn.AbstractClientConnAdapter.sendRequestEntity(AbstractClientConnAdapter.java:269)
05-30 09:46:16.020: E/AndroidRuntime(340):  at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:237)
05-30 09:46:16.020: E/AndroidRuntime(340):  at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:119)
05-30 09:46:16.020: E/AndroidRuntime(340):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:421)
05-30 09:46:16.020: E/AndroidRuntime(340):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-30 09:46:16.020: E/AndroidRuntime(340):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-30 09:46:16.020: E/AndroidRuntime(340):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-30 09:46:16.020: E/AndroidRuntime(340):  at chatclient.utility.HttpPostNetworkUtility.PerformPost(HttpPostNetworkUtility.java:43)
05-30 09:46:16.020: E/AndroidRuntime(340):  at chatclient.utility.ConnectionManager.ExecuteCommand(ConnectionManager.java:213)
05-30 09:46:16.020: E/AndroidRuntime(340):  at chatclient.utility.ConnectionManager.PollServer(ConnectionManager.java:192)
05-30 09:46:16.020: E/AndroidRuntime(340):  at chatclient.namespace.MainWindowActivity$ASyncPollServer.doInBackground(MainWindowActivity.java:402)
05-30 09:46:16.020: E/AndroidRuntime(340):  at chatclient.namespace.MainWindowActivity$ASyncPollServer.doInBackground(MainWindowActivity.java:1)

任何帮助都会非常感激!

谢谢!

1 个答案:

答案 0 :(得分:0)

This answer建议您在ThreadSafeClientConnManager内使用pollResult = ((ChatApplication) _instance.getApplication()).GetConnectionManager().PollServer();

我还会将((ChatApplication) _instance.getApplication())替换为onPreExecute或类似内容中初始化的成员变量。