当我使用android.support.v4.app.FragmentManager时,SupportmapFragment崩溃了

时间:2015-09-15 14:27:24

标签: android android-fragments nullpointerexception fragmentmanager supportmapfragment

我正在使用WorkAroundmapFragment,它在我的Application.earlier中扩展了SupportMapFragment,我使用了android.app.FragmentManager但它没有抛出任何错误,由于某些原因我转移到android.support.v4.app.FragmentManager,它正在抛出NullPointer异常。下面是我的代码片段

WorkAroundMapFragment.java

import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.widget.FrameLayout;

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;


import com.google.android.gms.maps.SupportMapFragment;

public class WorkAroundMapFragment extends SupportMapFragment {
    private OnTouchListener mListener;
    View layout;

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

            TouchableWrapper frameLayout = new TouchableWrapper(getActivity());

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

            assert layout != null;
            ((ViewGroup) layout).addView(frameLayout,
                    new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

            return layout;
        } catch (NotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        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);
      }
    }
}

我在另一个像

这样的片段中调用它
FragmentManager fm = ((FragmentActivity) getActivity())
                .getSupportFragmentManager();
        glmap = ((WorkAroundMapFragment) fm.findFragmentById(R.id.postmap)).getMap(); 
        glmap.clear();

这是我的XML代码

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center" >

         <fragment
                        android:id="@+id/postmap"
                        android:layout_width="@dimen/single_mapwidth"
                        android:layout_height="@dimen/single_mapheight"
                        android:layout_alignParentBottom="true"
                        class="com.devpoint.adapter.WorkAroundMapFragment"/>

        </LinearLayout>

它会抛出Null Pointer Exception。我可以解决这个问题吗?

这是我的logcat

09-15 20:07:45.329: E/AndroidRuntime(30570): FATAL EXCEPTION: main
09-15 20:07:45.329: E/AndroidRuntime(30570): java.lang.NullPointerException
09-15 20:07:45.329: E/AndroidRuntime(30570):    at com.devpoint.retailer.Retailers_Registration_Page.onViewCreated(Retailers_Registration_Page.java:201)
09-15 20:07:45.329: E/AndroidRuntime(30570):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
09-15 20:07:45.329: E/AndroidRuntime(30570):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
09-15 20:07:45.329: E/AndroidRuntime(30570):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
09-15 20:07:45.329: E/AndroidRuntime(30570):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)
09-15 20:07:45.329: E/AndroidRuntime(30570):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:483)
09-15 20:07:45.329: E/AndroidRuntime(30570):    at android.os.Handler.handleCallback(Handler.java:615)
09-15 20:07:45.329: E/AndroidRuntime(30570):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-15 20:07:45.329: E/AndroidRuntime(30570):    at android.os.Looper.loop(Looper.java:137)
09-15 20:07:45.329: E/AndroidRuntime(30570):    at android.app.ActivityThread.main(ActivityThread.java:4960)
09-15 20:07:45.329: E/AndroidRuntime(30570):    at java.lang.reflect.Method.invokeNative(Native Method)
09-15 20:07:45.329: E/AndroidRuntime(30570):    at java.lang.reflect.Method.invoke(Method.java:511)
09-15 20:07:45.329: E/AndroidRuntime(30570):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
09-15 20:07:45.329: E/AndroidRuntime(30570):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
09-15 20:07:45.329: E/AndroidRuntime(30570):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

显然你正在将一个片段调用到另一个片段中。将您的getSupportFragmentManager()替换为getChildFragmentManager()

getChildFragentManager()(来自支持库。)