【发布时间】:2016-03-21 03:41:30
【问题描述】:
看,我有以下代码:
我的行动:
final Intent intent = new Intent(getApplicationContext(), MyService.class)
.putExtra(UploadService.EXTRA_RESULT_RECEIVER, new ResultReceiver(null) {
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
String result = resultData.getString(MyService.EXTRA_RESULT_SUCCESS);
...
imageView.setBackgroundDrawable(bitmap);// here my code fails
}
})
我的服务:
Bundle b = new Bundle();
b.putString(EXTRA_RESULT_SUCCESS, response.toString());
resultReceiver.send(0, b);
我的应用程序在“imageView.setBackgroundDrawable(bitmap)”行失败,但出现以下异常:
11-13 16:25:38.986: ERROR/AndroidRuntime(3586): FATAL EXCEPTION: IntentService[MyService]
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
但是当我定义这样的接收器(使用处理程序)时,这不会发生:
new ResultReceiver(new Handler()){.../*here goes the same code as in the first example. nothing has been changed*/}
所以。当我通过默认处理程序时它不会失败。我问为什么?两种方式都调用了我的代码,但是当没有指定处理程序时,它会失败。 Handler有什么影响?
【问题讨论】: