0

我試圖在一個片段中實現谷歌地圖,但我沒有讓它工作。這裏就是我有這麼遠:谷歌地圖中的一個片段

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) 

回答

0

在你的MyAppFragment我認爲你需要請撥打getSupportFragmentManager()而不是getFragmentManager(),因爲您使用的是Google的支持套餐。看到Fragment documentation:異步

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

  • [...]

  • You must call getSupportFragmentManager() to get the FragmentManager

+0

嘗試使用.id.map))的GetMap(); 得到了同樣的錯誤。 – Wiillyx3

0

您應該獲得圖:。 MMAP =((SupportMapFragment)getActivity()getSupportFragmentManager()findFragmentById(R:

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; 
    } 

}