activity 处理程序和视图处理程序之间的区别 android
Difference between activity handler and view handler android
我试图从 Listview 的 OnItemClickListener 的 onItemClick() 方法向 activity 处理程序发送消息。
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Handler handler = view.getHandler();
String data = "MSG DATA";
Message msg = handler.obtainMessage(MSG_TYPE, data);
msg.sendToTarget();
}
我在 activity 中有一个处理程序作为
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
int msgId = msg.what;
Log.d(TAG,"GOT msg : " + msgId);
}
}
但我没有收到消息,应用程序崩溃了。
java.lang.ClassCastException: java.lang.String cannot be cast to android.view.View$AttachInfo$InvalidateInfo
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3175)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5289)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
来自 getHandler() 的文档:
A handler associated with the thread running the View. This handler can be used to pump events in the UI events queue.
因此,如果视图是在 activity 中创建的,它们都应该在同一个线程上。为什么我会崩溃?这是两个不同的处理程序吗?
Are these two different handlers?
它们是不同的 Handler
附加到同一个 UI 线程 Looper
。
So if the view is created in activity, they both should be on same thread. Why am I getting crash?
当您执行 handler.obtainMessage(MSG_TYPE, data);
时,它实际上将 target
设置为 handler
。因此,当您调用 msg.sendToTarget();
时,它会将消息发送到 View
的 Handler
,而不是 Activity
的。您收到此异常是因为 View
的 Handler
不知道如何处理 String
.
将您的 Activity
的 Handler
设置为 target
,您就不会发生崩溃。
我试图从 Listview 的 OnItemClickListener 的 onItemClick() 方法向 activity 处理程序发送消息。
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Handler handler = view.getHandler();
String data = "MSG DATA";
Message msg = handler.obtainMessage(MSG_TYPE, data);
msg.sendToTarget();
}
我在 activity 中有一个处理程序作为
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
int msgId = msg.what;
Log.d(TAG,"GOT msg : " + msgId);
}
}
但我没有收到消息,应用程序崩溃了。
java.lang.ClassCastException: java.lang.String cannot be cast to android.view.View$AttachInfo$InvalidateInfo
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3175)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5289)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
来自 getHandler() 的文档:
A handler associated with the thread running the View. This handler can be used to pump events in the UI events queue.
因此,如果视图是在 activity 中创建的,它们都应该在同一个线程上。为什么我会崩溃?这是两个不同的处理程序吗?
Are these two different handlers?
它们是不同的 Handler
附加到同一个 UI 线程 Looper
。
So if the view is created in activity, they both should be on same thread. Why am I getting crash?
当您执行 handler.obtainMessage(MSG_TYPE, data);
时,它实际上将 target
设置为 handler
。因此,当您调用 msg.sendToTarget();
时,它会将消息发送到 View
的 Handler
,而不是 Activity
的。您收到此异常是因为 View
的 Handler
不知道如何处理 String
.
将您的 Activity
的 Handler
设置为 target
,您就不会发生崩溃。