TouchInterceptor class in Android null pointer exception
I want to reuse the TouchInterceptor
from:
but got a NullPointerException on the line:
View dragger = item.findViewById(R.id.icon);
Trace:
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): java.lang.NullPointerException
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): at trackyt.android.client.ui.activities.TouchListView.onInterceptTouchEvent(TouchListView.java:134)
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:848)
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): at android.os.Looper.loop(Looper.java:123)
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): at java.lang.reflect.Method.invokeNative(Native Method)
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): at java.lang.reflect.Method.invoke(Method.java:507)
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): at com.android.internal.开发者_运维技巧os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-22 00:45:55.078: ERROR/AndroidRuntime(26116): at dalvik.system.NativeStart.main(Native Method)
I had a similar issue earlier - make sure you're calling it after onCreate()
has rendered the view from XML - i.e. after you have generated the view from XML at least once.
I had it before calling setContentView()
so it was returning a null
pointer.
In your list item you need to have an image view with id="@android/id:icon" to avoid this null pointer error!
精彩评论