Android空指针异常中的TouchInterceptor类

时间:2011-03-21 22:57:29

标签: android

我想重用TouchInterceptor来自: https://android.googlesource.com/platform/packages/apps/Music/+/master/src/com/android/music/TouchInterceptor.java

但在行上得到了NullPointerException:

View dragger = item.findViewById(R.id.icon);

跟踪:

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)

2 个答案:

答案 0 :(得分:0)

我之前遇到过类似问题 - 请确保在onCreate()从XML呈现视图后调用它 - 即在您从XML生成视图至少一次之后。

我在调用setContentView()之前已经有了它,所以它返回了一个null指针。

答案 1 :(得分:0)

在你的列表项中你需要有一个id =“@ android / id:icon”的图像视图,以避免这个空指针错误!