运行Android的TouchPaint示例程序时出错

时间:2012-01-18 04:51:55

标签: android graphics touch

我正在尝试使用Google Android示例提供的“TouchPaint”示例:

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/TouchPaint.html

创建了我自己的AndroidManifest.xml,在我编译并成功创建apk之前,我认为“GraphicsActivity.java”和“PictureLayout.java”是另外两个java文件。

我加载到Android手机后,显示空白屏幕。此时还没有发生任何事情,但是一旦我触摸屏幕,应用就会死亡,并在logcat输出中生成以下跟踪:

D/dalvikvm( 4939): GC_CONCURRENT freed 44K, 49% free 2779K/5379K, external 3286K/4104K, paused 4ms+2ms
D/dalvikvm(  365): GC_CONCURRENT freed 1102K, 54% free 3271K/7047K, external 2612K/3262K, paused 4ms+4ms
I/ActivityManager(  266): Displayed com.example.android.apis.graphics/.TouchPaint: +382ms
D/AndroidRuntime( 4939): Shutting down VM
W/dalvikvm( 4939): threadid=1: thread exiting with uncaught exception (group=0x2aac8578)
E/AndroidRuntime( 4939): FATAL EXCEPTION: main
E/AndroidRuntime( 4939): java.lang.NoSuchMethodError: android.view.MotionEvent.getButtonState
E/AndroidRuntime( 4939):    at com.example.android.apis.graphics.TouchPaint$PaintView.onTouchOrHoverEvent(TouchPaint.java:346)
E/AndroidRuntime( 4939):    at com.example.android.apis.graphics.TouchPaint$PaintView.onTouchEvent(TouchPaint.java:337)
E/AndroidRuntime( 4939):    at android.view.View.dispatchTouchEvent(View.java:3952)
E/AndroidRuntime( 4939):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:961)
E/AndroidRuntime( 4939):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:961)
E/AndroidRuntime( 4939):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1711)
E/AndroidRuntime( 4939):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1145)
E/AndroidRuntime( 4939):    at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
E/AndroidRuntime( 4939):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1695)
E/AndroidRuntime( 4939):    at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2217)
E/AndroidRuntime( 4939):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1901)
E/AndroidRuntime( 4939):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 4939):    at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 4939):    at android.app.ActivityThread.main(ActivityThread.java:3701)
E/AndroidRuntime( 4939):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 4939):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 4939):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
E/AndroidRuntime( 4939):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
E/AndroidRuntime( 4939):    at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(  266):   Force finishing activity com.example.android.apis.graphics/.TouchPaint
W/ActivityManager(  266): Activity pause timeout for HistoryRecord{2afed900 com.example.android.apis.graphics/.TouchPaint}
I/InputDispatcher(  266): Application is not responding: Window{2b3f4d00 com.example.android.apis.graphics/com.example.android.apis.graphics.TouchPaint paused=false}.  5002.0ms since event, 5001.8ms since wait started
I/InputDispatcher(  266): Dropping event because the pointer is not down.
I/WindowManager(  266): Input event dispatching timed out sending to com.example.android.apis.graphics/com.example.android.apis.graphics.TouchPaint
W/ActivityManager(  266): Activity destroy timeout for HistoryRecord{2afed900 com.example.android.apis.graphics/.TouchPaint}
D/lights  (  266): set_light_backlight: brightness=20
I/ActivityManager(  266): No longer want com.facebook.katana (pid 4319): hidden #16
W/ActivityManager(  266): Scheduling restart of crashed service com.facebook.katana/.service.UploadManager in 5000ms

我很担心崩溃的可能原因是什么?

2 个答案:

答案 0 :(得分:1)

此方法仅在API级别14可用,在清单中将minSdkVersion设置为14

public final int getButtonState ()

Since: API Level 14
Gets the state of all buttons that are pressed such as a mouse or stylus button.
Returns

The button state.

答案 1 :(得分:0)

我找到了答案:

http://developer.android.com/resources/dashboard/platform-versions.html

http://developer.android.com/guide/appendix/api-levels.html

从网页表中,我知道我的手机使用的是Android 2.3.4,因此SDK的最大目标API应为10.因此,我没有使用Android-14的TouchPaint.java示例,而是复制了TouchPaint来自android-10的.java,在我的AndroidManifest.xml文件中将minSDK版本更改为10:

   <uses-sdk android:minSdkVersion="10"/>

一切正常!!感谢Rajdeep Dua !!!