gpt4 book ai didi

android - onTouchEvent 通过代码通过 Instrumentation

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:10:39 24 4
gpt4 key购买 nike

我要完成以下任务:

I have two buttons. When first button is pressed,
it will fire onTouchEvent on second button, thus pressing second button.

这是触发事件的代码摘录:

int test1[] = new int[2];
button2.getLocationInWindow(test1); //--->getting coordinates of second button
Instrumentation m_Instrumentation = new Instrumentation();
//firing event
m_Instrumentation.sendPointerSync(MotionEvent.obtain(android.os.SystemClock.uptimeMillis(),android.os.SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN,test1[0]+10, test1[1]+10, 0));

注意:我使用genymotion模拟器。

错误日志:

07-08 12:47:38.743: E/InputEventReceiver(6849): Exception dispatching input event.
07-08 12:47:38.743: E/MessageQueue-JNI(6849): Exception in MessageQueue callback: handleReceiveCallback
07-08 12:47:38.743: E/MessageQueue-JNI(6849): java.lang.RuntimeException: This method can not be called from the main application thread
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at android.app.Instrumentation.validateNotAppThread(Instrumentation.java:1651)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at android.app.Instrumentation.sendPointerSync(Instrumentation.java:933)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at com.example.touchtest1.MainActivity.fireEvent(MainActivity.java:55)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at com.example.touchtest1.MainActivity$MyTouchListener.onTouch(MainActivity.java:75)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at android.view.View.dispatchTouchEvent(View.java:7701)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.dispatchTouchEvent(ActionBarActivityDelegateICS.java:260)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at android.view.View.dispatchPointerEvent(View.java:7886)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
07-08 12:47:38.743: E/MessageQueue-JNI(6849): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)

然而,它并没有按预期进行。哪里会出问题?

问候

最佳答案

基于错误和the excerpt at the bottom of this documentation ,看起来您不能在主线程(换句话说,UI 线程)上运行它。

一个可能的解决方案是在另一个线程中运行它。它看起来像这样:

int test1[] = new int[2];
button2.getLocationInWindow(test1); //--->getting coordinates of second button
final Instrumentation m_Instrumentation = new Instrumentation();
//firing event

new Thread(new Runnable() {
@Override
public void run() {
m_Instrumentation.sendPointerSync(MotionEvent.obtain(
android.os.SystemClock.uptimeMillis(),
android.os.SystemClock.uptimeMillis(),
MotionEvent.ACTION_DOWN,test1[0]+10, test1[1]+10, 0));
}
}).start();

请注意,我将您的 m_Instrumentation 更改为 final 变量,以便它可以在不同的线程中使用。

关于android - onTouchEvent 通过代码通过 Instrumentation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24632172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com