Android:无法想象这个例外

时间:2015-06-04 15:36:14

标签: android nullpointerexception

这是我的例外日志

java.lang.NullPointerException
        at android.view.GestureDetector.onTouchEvent(GestureDetector.java:587)
        at android.widget.Gallery.onTouchEvent(Gallery.java:937)
        at android.view.View.dispatchTouchEvent(View.java:5541)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1951)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1712)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1912)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1371)
        at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
        at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:59)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1860)
        at android.view.View.dispatchPointerEvent(View.java:5721)
        at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2890)
        at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2466)
        at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:845)
        at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2475)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4424)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
        at dalvik.system.NativeStart.main(Native Method)

点击按钮后会发生这种情况。我可以调试onClick事件以查看它是否按预期执行并使用添加和删除视图替换GroupView容器中的视图。

我在使用GestureDetector的代码中没有位置,也没有使用任何外部库。

我在Bluestacks和appcompact v7:22.2.0

工作

任何想法可能是什么?或者我还能测试什么?

我的点击监听器代码:

    fancyCoverFlow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Command command = Factory.inject(lobbyModel.items.get(position).commandClass);
            command.execute();
        }
    });

来自调试器的线程转储

<1> main@831927632808" prio=5 runnable
  java.lang.Thread.State: RUNNABLE
      at myClass.onItemClick(LobbyScreen.java:47)
      at android.widget.AdapterView.performItemClick(AdapterView.java:292)
      at android.widget.Gallery.onSingleTapUp(Gallery.java:960)
      at android.view.GestureDetector.onTouchEvent(GestureDetector.java:568)
      at android.widget.Gallery.onTouchEvent(Gallery.java:937)
      at android.view.View.dispatchTouchEvent(View.java:5541)
      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1951)
      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1712)
      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
      at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1912)
      at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1371)
      at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
      at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:59)
      at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1860)
      at android.view.View.dispatchPointerEvent(View.java:5721)
      at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2890)
      at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2466)
      at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:845)
      at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2475)
      at android.os.Handler.dispatchMessage(Handler.java:99)
      at android.os.Looper.loop(Looper.java:137)
      at android.app.ActivityThread.main(ActivityThread.java:4424)
      at java.lang.reflect.Method.invokeNative(Method.java:-1)
      at java.lang.reflect.Method.invoke(Method.java:511)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
      at dalvik.system.NativeStart.main(NativeStart.java:-1)

Solved

2 个答案:

答案 0 :(得分:4)

基于@rhashimoto,我为旧版本添加了一个解决方法。只需覆盖视图类并添加try catch。

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    try {
        return super.dispatchTouchEvent(event);
    }
    catch (Exception ignored){
        return true;
    }
}

这可能不是最好的解决方案,但它解决了我的问题。

答案 1 :(得分:1)

您是在冰淇淋三明治还是早期系统上进行测试?它看起来像是在Jelly Bean中修复的Android bug。

这是ICS code

// Hold the event we obtained above - listeners may have changed the original.
mPreviousUpEvent = currentUpEvent;
mVelocityTracker.recycle();
mVelocityTracker = null;

这是Jelly Bean code

// Hold the event we obtained above - listeners may have changed the original.
mPreviousUpEvent = currentUpEvent;
if (mVelocityTracker != null) {
    // This may have been cleared when we called out to the
    // application above.
    mVelocityTracker.recycle();
    mVelocityTracker = null;
}

了解Jelly Bean现在如何为mVelocityTracker测试null?我认为这就是你的崩溃发生的地方。

看起来这个bug已经遇到了很多其他时间,看起来特定于ICS: