我有一个奇怪的错误与ViewPager - 当我慢慢刷它一切都很好,但是当我快速崩溃它崩溃。这个导致崩溃(NullPointerException):
Bitmap hotspots = Bitmap.createBitmap(img.getDrawingCache());
整个方法:
public int getHotspotColor(int hotspotId, int x, int y) {
ImageView img = (ImageView) itemView.findViewById(hotspotId);
img.setDrawingCacheEnabled(true);
//img.buildDrawingCache(); //update
Bitmap hotspots = Bitmap.createBitmap(img.getDrawingCache());
img.setDrawingCacheEnabled(false);
return hotspots.getPixel(x, y);
}
主要是经过两三次快节奏的滑动后,它会崩溃。是慢吗?
logcat的:
12-18 18:50:21.831: E/InputEventReceiver(12924): Exception dispatching input event.
12-18 18:50:21.832: E/MessageQueue-JNI(12924): Exception in MessageQueue callback: handleReceiveCallback
12-18 18:50:21.846: E/MessageQueue-JNI(12924): java.lang.NullPointerException
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.graphics.Bitmap.createBitmap(Bitmap.java:639)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at pl.dajsiezlapac.apka.ViewPagerAdapter.getHotspotColor(ViewPagerAdapter.java:156)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at pl.dajsiezlapac.apka.ViewPagerAdapter$1.onTouch(ViewPagerAdapter.java:78)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.View.dispatchTouchEvent(View.java:7731)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2212)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2240)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.app.Activity.dispatchTouchEvent(Activity.java:2466)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.View.dispatchPointerEvent(View.java:7916)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4023)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3902)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3471)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3479)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3471)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3479)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5657)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5588)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5559)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5737)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.os.MessageQueue.nativePollOnce(Native Method)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.os.MessageQueue.next(MessageQueue.java:138)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.os.Looper.loop(Looper.java:123)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at android.app.ActivityThread.main(ActivityThread.java:5086)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at java.lang.reflect.Method.invokeNative(Native Method)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at java.lang.reflect.Method.invoke(Method.java:515)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-18 18:50:21.846: E/MessageQueue-JNI(12924): at dalvik.system.NativeStart.main(Native Method)
12-18 18:50:21.846: D/AndroidRuntime(12924): Shutting down VM
12-18 18:50:21.848: W/dalvikvm(12924): threadid=1: thread exiting with uncaught exception (group=0x41704d40)
12-18 18:50:21.856: E/AndroidRuntime(12924): FATAL EXCEPTION: main
12-18 18:50:21.856: E/AndroidRuntime(12924): Process: pl.dajsiezlapac.apka, PID: 12924
12-18 18:50:21.856: E/AndroidRuntime(12924): java.lang.NullPointerException
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.graphics.Bitmap.createBitmap(Bitmap.java:639)
12-18 18:50:21.856: E/AndroidRuntime(12924): at pl.dajsiezlapac.apka.ViewPagerAdapter.getHotspotColor(ViewPagerAdapter.java:156)
12-18 18:50:21.856: E/AndroidRuntime(12924): at pl.dajsiezlapac.apka.ViewPagerAdapter$1.onTouch(ViewPagerAdapter.java:78)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.View.dispatchTouchEvent(View.java:7731)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2212)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2240)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 18:50:21.856: E/AndroidRuntime(12924): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
12-18 18:50:21.856: E/AndroidRuntime(12924): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.app.Activity.dispatchTouchEvent(Activity.java:2466)
12-18 18:50:21.856: E/AndroidRuntime(12924): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.View.dispatchPointerEvent(View.java:7916)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4023)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3902)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3471)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3479)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3471)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3479)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5657)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5588)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5559)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5737)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.os.MessageQueue.nativePollOnce(Native Method)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.os.MessageQueue.next(MessageQueue.java:138)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.os.Looper.loop(Looper.java:123)
12-18 18:50:21.856: E/AndroidRuntime(12924): at android.app.ActivityThread.main(ActivityThread.java:5086)
12-18 18:50:21.856: E/AndroidRuntime(12924): at java.lang.reflect.Method.invokeNative(Native Method)
12-18 18:50:21.856: E/AndroidRuntime(12924): at java.lang.reflect.Method.invoke(Method.java:515)
12-18 18:50:21.856: E/AndroidRuntime(12924): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-18 18:50:21.856: E/AndroidRuntime(12924): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-18 18:50:21.856: E/AndroidRuntime(12924): at dalvik.system.NativeStart.main(Native Method)
12-18 18:50:23.113: I/Process(12924): Sending signal. PID: 12924 SIG: 9
我使用getHotspotId()方法的片段:
@Override
public Object instantiateItem(ViewGroup container, final int position) {
// Declare Variables
final ImageView imggun;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.viewpager_item, container,
false);
FrameLayout layout = (FrameLayout) itemView.findViewById(R.id.fl1);
layout.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent ev) {
final int action = ev.getAction();
final int evX = (int) ev.getX();
final int evY = (int) ev.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
int touchColor = getHotspotColor(R.id.buttons, evX, evY);
ColorTool ct = new ColorTool(); //line 83
int tolerance = 25;
...
}
答案 0 :(得分:1)
应用程序崩溃,因为它是在MotionEvent.ACTION_DOWN事件中,每次将手指放在屏幕上时都会激活它。
另一方面,如果你把它放在MotionEvent.ACTION_UP部分,如果你把手指放在屏幕上并轻扫,那么就不会注意到ACTION_UP,这就是getHotspotId()的位置。
所以,代码是正确的 - 它的位置并不那么完整。谢谢你的帮助!
答案 1 :(得分:0)
您的视图的绘图缓存尚未就绪。您需要确保视图将绘制缓存,首先调用img.setDrawingCacheEnabled(true);
然后调用您的行。