在支持映射片段上处理触摸事件(空对象引用上的OnTouchListener.onTouch())时,返回空指针

时间:2019-05-06 15:15:25

标签: android nullpointerexception supportmapfragment android-touch-event

我有一个android片段,它是一个嵌套的滚动视图。该视图包含一张地图(Google地图)。为了能够在地图内移动而无需滚动嵌套的滚动视图,我一直在寻找一种扩展SupportMapFragment并处理触摸情况的实现。支持地图片段代码:

public class WorkaroundMapFragment extends SupportMapFragment {
private OnTouchListener mListener;

@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstance) {
    View layout = super.onCreateView(layoutInflater, viewGroup, savedInstance);

    TouchableWrapper frameLayout = new TouchableWrapper(getActivity());

    frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));

    ((ViewGroup) layout).addView(frameLayout,
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

    return layout;
}

public void setListener(OnTouchListener listener) {
    mListener = listener;
}

public interface OnTouchListener {
    void onTouch();
}

public class TouchableWrapper extends FrameLayout {

    public TouchableWrapper(Context context) {
        super(context);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mListener.onTouch();
                break;
            case MotionEvent.ACTION_UP:
                mListener.onTouch();
                break;
        }
        return super.dispatchTouchEvent(event);
    }
}}

在xml上:

  <fragment
        android:id="@+id/fragment_map"
        class="inesc_id.pt.motivandroid.myTrips.WorkaroundMapFragment"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_alignParentStart="true"
        android:tag="fragment_map"
        app:layout_constraintBottom_toTopOf="@+id/textView20"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

OnMapReady代码:

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mMap.getUiSettings().setZoomControlsEnabled(true);


    ((WorkaroundMapFragment) getChildFragmentManager().findFragmentById(R.id.fragment_map))
            .setListener(new WorkaroundMapFragment.OnTouchListener() {
                @Override
                public void onTouch()
                {
                    nestedScrollView.requestDisallowInterceptTouchEvent(true);
                }
            });

    View mapView = ((WorkaroundMapFragment) getChildFragmentManager().findFragmentById(R.id.fragment_map)).getView();
    mapView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

    height = DensityUtil.convertDpIntoPx(getContext(), mapView.getMeasuredHeight());
    width = DensityUtil.convertDpIntoPx(getContext(), mapView.getMeasuredWidth());

    MotivMapUtils.drawRouteOnMap(fullTripBeingValidated, mMap, getContext(), height, width);

}

OnCreate代码:

    if (mMap == null) {
        SupportMapFragment mapFragment = (WorkaroundMapFragment) getChildFragmentManager().findFragmentById(R.id.fragment_map);
        mapFragment.getMapAsync(this);
    }

OnResume:

if (mMap != null){
        mMap.clear();
        MotivMapUtils.drawRouteOnMap(fullTripBeingValidated, mMap, getContext(), height, width);
    }

我无法复制此错误,但是有几次通过crashlytics报告给我:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke interface method 'void inesc_id.pt.motivandroid.myTrips.WorkaroundMapFragment$OnTouchListener.onTouch()' on a null object reference
   at inesc_id.pt.motivandroid.myTrips.WorkaroundMapFragment$TouchableWrapper.dispatchTouchEvent(WorkaroundMapFragment.java:49)
   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2456)
   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2456)
   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2456)
   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2456)
   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2456)
   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2456)
   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2456)
   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2456)
   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2456)
   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2456)
   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2456)
   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2456)
   at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2831)
   at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1863)
   at android.app.Activity.dispatchTouchEvent(Activity.java:3046)
   at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
   at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2792)
   at android.view.View.dispatchPointerEvent(View.java:10228)
   at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5344)
   at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5180)
   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4620)
   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4673)
   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4639)
   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4781)
   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4647)
   at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4838)
   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4620)
   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4673)
   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4639)
   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4647)
   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4620)
   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7306)
   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7184)
   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7145)
   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7416)
   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
   at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
   at android.os.MessageQueue.next(MessageQueue.java:323)
   at android.os.Looper.loop(Looper.java:143)
   at android.app.ActivityThread.main(ActivityThread.java:7224)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

关于OnTouchListener如何变为空的任何想法?预先感谢!

0 个答案:

没有答案