我试图通过从Activity调用的Asynctask访问REMOTE android服务。
我遇到了一个问题,我认为这是由线程重用引起的。我在AsyncTask中使用Lopper,当它获得一个线程时,它已经被之前调用的远程服务分配了,我得到一个错误,即每个线程只允许一个looper。我不认为这是因为我试图在错误的线程上访问UI。
我可以通过一些非常简单的代码来模仿这个问题。我在活动上创建了一个按钮,然后按下它6次。希望有人可以建议我如何解决这个问题。我已经尝试检查looper不是null,但这只会让我遇到另一个问题,所以我想尝试解决这个问题。
这是我的活动
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonClick(View view)
{
MyAsychTask myAsychTask = new MyAsychTask() ;
myAsychTask.execute() ;
}}
AsyncTask
public class MyAsychTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
Log.wtf("Thead details", Thread.currentThread().getName() + " : " + Thread.currentThread().getState().toString() + " : Looper def: " + ((Looper.myLooper()==null) ? "none" : "looper") ) ;
Looper.prepare();
Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
// in real app process incoming messages here then quit looper
Log.wtf("executing message", " ") ;
this.getLooper().quit() ;
}
};
new simulateREMOTEservice(myHandler) ;
Looper.loop() ;
Log.wtf("exited looper", " ") ;
return null ;
}}
class simulateREMOTEservice{
public simulateREMOTEservice(Handler myHandler)
{
Log.wtf("simulating REMOTE service", " ") ;
myHandler.sendEmptyMessage(0) ;
}}
日志文件是
10-18 19:00:12.420: A/Thead details(3081): AsyncTask #1 : RUNNABLE : Looper def: none
10-18 19:00:12.420: A/simulating REMOTE service(3081):
10-18 19:00:12.420: A/executing message(3081):
10-18 19:00:12.420: A/exited looper(3081):
10-18 19:00:14.280: A/Thead details(3081): AsyncTask #2 : RUNNABLE : Looper def: none
10-18 19:00:14.290: A/simulating REMOTE service(3081):
10-18 19:00:14.290: A/executing message(3081):
10-18 19:00:14.300: A/exited looper(3081):
10-18 19:00:16.140: A/Thead details(3081): AsyncTask #3 : RUNNABLE : Looper def: none
10-18 19:00:16.150: A/simulating REMOTE service(3081):
10-18 19:00:16.150: A/executing message(3081):
10-18 19:00:16.150: A/exited looper(3081):
10-18 19:00:17.830: A/Thead details(3081): AsyncTask #4 : RUNNABLE : Looper def: none
10-18 19:00:17.840: A/simulating REMOTE service(3081):
10-18 19:00:17.840: A/executing message(3081):
10-18 19:00:17.840: A/exited looper(3081):
10-18 19:00:19.480: A/Thead details(3081): AsyncTask #5 : RUNNABLE : Looper def: none
10-18 19:00:19.480: A/simulating REMOTE service(3081):
10-18 19:00:19.490: A/executing message(3081):
10-18 19:00:19.490: A/exited looper(3081):
10-18 19:00:21.810: A/Thead details(3081): AsyncTask #1 : RUNNABLE : Looper def: looper
10-18 19:00:21.820: E/AndroidRuntime(3081): FATAL EXCEPTION: AsyncTask #1
10-18 19:00:21.820: E/AndroidRuntime(3081): java.lang.RuntimeException: An error occured while executing doInBackground()
10-18 19:00:21.820: E/AndroidRuntime(3081): at android.os.AsyncTask$3.done(AsyncTask.java:266)
10-18 19:00:21.820: E/AndroidRuntime(3081): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-18 19:00:21.820: E/AndroidRuntime(3081): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-18 19:00:21.820: E/AndroidRuntime(3081): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-18 19:00:21.820: E/AndroidRuntime(3081): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-18 19:00:21.820: E/AndroidRuntime(3081): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
10-18 19:00:21.820: E/AndroidRuntime(3081): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
10-18 19:00:21.820: E/AndroidRuntime(3081): at java.lang.Thread.run(Thread.java:1020)
10-18 19:00:21.820: E/AndroidRuntime(3081): Caused by: java.lang.RuntimeException: Only one Looper may be created per thread
10-18 19:00:21.820: E/AndroidRuntime(3081): at android.os.Looper.prepare(Looper.java:76)
10-18 19:00:21.820: E/AndroidRuntime(3081): at com.example.testlooper.MyAsychTask.doInBackground(MyAsychTask.java:15)
10-18 19:00:21.820: E/AndroidRuntime(3081): at com.example.testlooper.MyAsychTask.doInBackground(MyAsychTask.java:1)
10-18 19:00:21.820: E/AndroidRuntime(3081): at android.os.AsyncTask$2.call(AsyncTask.java:252)
10-18 19:00:21.820: E/AndroidRuntime(3081): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-18 19:00:21.820: E/AndroidRuntime(3081): ... 4 more
答案 0 :(得分:0)
根据我的评论,请查看网址code.google.com/p/android/issues/detail?id=20915,虽然我怀疑真正的问题不在于AsyncTask线程池,但事实是你创建了一个Looper,你似乎无法删除它或用新的替换它。