更新的
我读了很多主题,但没有帮助或我不知道该怎么做。我失去了我不知道为什么。我只是在学习,所以请帮帮我。
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
at com.example.text.fragments.MapFragment.initilizeMap(MapFragment.java:52)
at com.example.text.fragments.MapFragment.onViewCreated(MapFragment.java:46)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:971)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:488)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:249)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792)
at android.view.Choreographer.doCallbacks(Choreographer.java:596)
at android.view.Choreographer.doFrame(Choreographer.java:556)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:778)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
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:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
这是我实现getmap的片段
public class MapFragment extends Fragment implements OnMapReadyCallback{
private GoogleMap googleMap;
/**
* function to load map. If map is not created it will create it for you
* */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.map_fragment, container, false);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
// here, as doc say, the map can be initialized, or the service is not available
initilizeMap();
}
private void initilizeMap() {
if (googleMap == null) {
((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(
R.id.map)).getMapAsync(this);
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getActivity().getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
public void onMapReady(GoogleMap mGoogleMap) {
googleMap = mGoogleMap;
}
}
这是map_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
这是我的名单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test" >
<permission
android:name="com.example.test.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.test.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:supportsRtl="true">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBUeq6ANw9MyK9vwB4phnSOkp6e_UccN4E" />
</application>
</manifest>
我使用滑动标签,在一个标签必须有地图。我在viewpageradapter中的代码的这一部分创建了这个地图:
@Override
public Fragment getItem(int position) {
if(mNumbOfTabs == 3) {
if (position == 0) {
A a = new A();
return a;
} else if (position == 1) {
B b = new B();
return b;
} else {
MapFragment mapFragment = new MapFragment();
return mapFragment;
}
}
最佳答案
因为6.5版本的play服务googlerecommends using of asynchronous getting of GoogleMap和getMap()
现在已经被弃用。正如stkent所提到的,你必须打电话给getMapAsync(this)
。
由于此方法是异步的,因此需要对此方法进行回调,这就是为什么还必须使片段实现OnMapReadyCallback
,并且在这些回调的onMapReady()
方法中,可以获得对映射的引用。
同时将行((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
更改为((SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
,因为在片段内部必须使用childfragmentmanager。
关于android - Google Maps getMap返回null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30077101/