包含MapFragment的Android Fragment无效

时间:2015-03-31 14:43:33

标签: android supportmapfragment mapfragment

我想在我的TabedActivity中实现MapFragment。 所有这一切仍然正常,当我尝试访问地图以修改它时出现问题。

我希望你能帮帮我。

public class RecordTrailFragment extends Fragment implements OnMapReadyCallback {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnRecordTrailInteractionListener mListener;

private GoogleMap map;

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment RecordTrailFragment.
 */
// TODO: Rename and change types and number of parameters
public static RecordTrailFragment newInstance(String param1, String param2) {
    RecordTrailFragment fragment = new RecordTrailFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

public RecordTrailFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

        //map = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapFragment)).getMap();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    super.onCreateView(inflater, container, savedInstanceState);
    return inflater.inflate(R.layout.fragment_record_trail, container, false);
}

public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.OnRecordTrailInteractionListener(uri);
    }
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnRecordTrailInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

@Override
public void onMapReady(GoogleMap googleMap) {

}

public interface OnRecordTrailInteractionListener {
    // TODO: Update argument type and name
    public void OnRecordTrailInteractionListener(Uri uri);
}

} 片段调用MapFragment出现的位置

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.work_for_dogs.wfdmantrailing.RecordTrailFragment"
android:orientation="vertical">


<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/mapFragment"
    android:layout_gravity="center_horizontal" />

布局

03-31 16:41:18.393  17376-17376/de.work_for_dogs.wfdmantrailing E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: de.work_for_dogs.wfdmantrailing, PID: 17376
java.lang.NullPointerException
        at de.work_for_dogs.wfdmantrailing.RecordTrailFragment.onCreate(RecordTrailFragment.java:66)
        at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
        at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:844)
        at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:859)
        at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1071)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
        at android.view.View.measure(View.java:16538)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16538)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
        at android.view.View.measure(View.java:16538)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16538)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1406)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:697)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
        at android.view.View.measure(View.java:16538)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2374)
        at android.view.View.measure(View.java:16538)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1964)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1160)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1342)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1047)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5896)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:544)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5105)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
        at dalvik.system.NativeStart.main(Native Method)

至少是错误

谢谢, 魔

0 个答案:

没有答案