找到你要的答案

Q:Google maps in a fragment [duplicate]

Q:谷歌地图片段[复制]

This question already has an answer here:

I'm trying to implement Google Map in a fragment but I couln't make it work. Here's what I got so far:

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Fragment fragment_map = new MyMapFragment();
            FragmentManager fragmentManager =getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container_body, fragment_map);
            fragmentTransaction.commit();
    }
}

MyMapFragment.java:

public class MyMapFragment extends Fragment {

        private GoogleMap mMap;

        public MyMapFragment() {
        }

        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_map, container, false);
            mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
            return rootView;
        }
}

fragment_map.xml

<RelativeLayout 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=".MainActivity" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>

However, when I launch this, I get the following error:

08-15 13:10:28.553  10818-10818/com.mts.campr E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mts.campr, PID: 10818
    java.lang.RuntimeException: Unable to resume activity {com.mts.campr/com.mts.campr.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3017)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3048)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1365)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5289)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference
            at com.mts.campr.MyMapFragment.onCreateView(MyMapFragment.java:30)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentActivity.onResume(FragmentActivity.java:426)
            at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1257)
            at android.app.Activity.performResume(Activity.java:6076)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3006)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3048)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1365)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5289)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

这个问题在这里已经有了答案:

我试图在一个片段实现谷歌地图但我无法让它工作。这是我迄今为止得到的:

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Fragment fragment_map = new MyMapFragment();
            FragmentManager fragmentManager =getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container_body, fragment_map);
            fragmentTransaction.commit();
    }
}

MyMapFragment.java:

public class MyMapFragment extends Fragment {

        private GoogleMap mMap;

        public MyMapFragment() {
        }

        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_map, container, false);
            mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
            return rootView;
        }
}

fragment_map.xml

<RelativeLayout 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=".MainActivity" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>

然而,当我启动这个,我得到以下错误:

08-15 13:10:28.553  10818-10818/com.mts.campr E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mts.campr, PID: 10818
    java.lang.RuntimeException: Unable to resume activity {com.mts.campr/com.mts.campr.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3017)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3048)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1365)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5289)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference
            at com.mts.campr.MyMapFragment.onCreateView(MyMapFragment.java:30)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentActivity.onResume(FragmentActivity.java:426)
            at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1257)
            at android.app.Activity.performResume(Activity.java:6076)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3006)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3048)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1365)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5289)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
answer1: 回答1:

In your MyAppFragment I think you need to call getSupportFragmentManager() instead of getFragmentManager() because you are using Google's support package. see the Fragment documentation:

The main differences when using this support version instead of the framework version are:

  • [...]

  • You must call getSupportFragmentManager() to get the FragmentManager

在你的myappfragment我认为你需要打电话给getsupportfragmentmanager()代替getfragmentmanager()因为您使用的是谷歌的支持包。见片段文档:

使用此支持版本而不是框架版本的主要区别是:

  • […]

  • 你必须getsupportfragmentmanager()得到FragmentManager

answer2: 回答2:

You should obtain map asynchronously:

public class MyMapFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_map, container, false);
        SupportMapFragment mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
        mapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap map) {
               // do want you want with map
            }
        return rootView;
    }

}

你应该异步获取地图:

public class MyMapFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_map, container, false);
        SupportMapFragment mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
        mapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap map) {
               // do want you want with map
            }
        return rootView;
    }

}

java  android  google-maps  android-fragments  google-maps-android-api-2