我正在通过片段类实现OnTouch侦听器。
public class ButtonFragment extends Fragment implements OnTouchListener
Overriden onTouch功能看起来像。
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.buttonUp :
{
mAction = new Thread(){
@Override
public void run()
{
try {
ChartJNI3D.cameraPos(1.0f, 'U');
mAction.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
if(event.getAction() == MotionEvent.ACTION_DOWN)
mAction.start();
else if (event.getAction() == MotionEvent.ACTION_UP)
mAction.stop();
}
break;
等等。
问题是只需要一个OnClick事件并调用该函数。
但是当它遇到MotionEvent.ACTION_UP
时,应用程序崩溃并出现NULL指针异常。
如果有人需要,可以发布日志。
01-08 17:59:10.771: E/AndroidRuntime(3926): FATAL EXCEPTION: Thread-344
01-08 17:59:10.771: E/AndroidRuntime(3926): java.lang.IllegalMonitorStateException: object not locked by thread before wait()
01-08 17:59:10.771: E/AndroidRuntime(3926): at java.lang.Object.wait(Native Method)
01-08 17:59:10.771: E/AndroidRuntime(3926): at java.lang.Object.wait(Object.java:364)
01-08 17:59:10.771: E/AndroidRuntime(3926): at com.example.sample3dchart.ButtonFragment$1.run(ButtonFragment.java:53)
01-08 17:59:10.816: E/InputEventReceiver(3926): Exception dispatching input event.
01-08 17:59:10.816: E/MessageQueue-JNI(3926): Exception in MessageQueue callback: handleReceiveCallback
01-08 17:59:10.821: E/MessageQueue-JNI(3926): java.lang.UnsupportedOperationException
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at java.lang.Thread.stop(Thread.java:1076)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at java.lang.Thread.stop(Thread.java:1063)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at com.example.sample3dchart.ButtonFragment.onTouch(ButtonFragment.java:64)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.View.dispatchTouchEvent(View.java:7241)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.View.dispatchPointerEvent(View.java:7426)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.os.MessageQueue.nativePollOnce(Native Method)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.os.MessageQueue.next(MessageQueue.java:125)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.os.Looper.loop(Looper.java:124)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at java.lang.reflect.Method.invoke(Method.java:511)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-08 17:59:10.821: E/MessageQueue-JNI(3926): at dalvik.system.NativeStart.main(Native Method)
而不是
try {
ChartJNI3D.cameraPos(1.0f, 'U');
mAction.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我做
try {
ChartJNI3D.cameraPos(1.0f, 'U');
mAction.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
日志
01-08 18:01:28.371: E/AndroidRuntime(4125): FATAL EXCEPTION: main
01-08 18:01:28.371: E/AndroidRuntime(4125): java.lang.UnsupportedOperationException
01-08 18:01:28.371: E/AndroidRuntime(4125): at java.lang.Thread.stop(Thread.java:1076)
01-08 18:01:28.371: E/AndroidRuntime(4125): at java.lang.Thread.stop(Thread.java:1063)
01-08 18:01:28.371: E/AndroidRuntime(4125): at com.example.sample3dchart.ButtonFragment.onTouch(ButtonFragment.java:64)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.View.dispatchTouchEvent(View.java:7241)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-08 18:01:28.371: E/AndroidRuntime(4125): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
01-08 18:01:28.371: E/AndroidRuntime(4125): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
01-08 18:01:28.371: E/AndroidRuntime(4125): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.View.dispatchPointerEvent(View.java:7426)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.os.MessageQueue.nativePollOnce(Native Method)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.os.MessageQueue.next(MessageQueue.java:125)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.os.Looper.loop(Looper.java:124)
01-08 18:01:28.371: E/AndroidRuntime(4125): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-08 18:01:28.371: E/AndroidRuntime(4125): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 18:01:28.371: E/AndroidRuntime(4125): at java.lang.reflect.Method.invoke(Method.java:511)
01-08 18:01:28.371: E/AndroidRuntime(4125): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-08 18:01:28.371: E/AndroidRuntime(4125): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-08 18:01:28.371: E/AndroidRuntime(4125): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
您崩溃的原因是因为您通过调用已弃用的stop()
方法来结束您的线程而滥用Java Thread对象。
private MyThread mAction;
private class MyThread extends Thread() {
volatile boolean keepRunning = true;
@Override
public void run() {
try {
ChartJNI3D.cameraPos(1.0f, 'U');
while(keepRunning);
} catch (InterruptedException e) {}
}
public void quit() {
keepRunning = false;
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(v.getId()) {
case R.id.buttonUp: {
if(event.getAction() == MotionEvent.ACTION_DOWN)
mAction = new MyThread();
mAction.start();
else if (event.getAction() == MotionEvent.ACTION_UP)
mAction.quit();
}
break;
}
}