问题描述
我的应用程序的 fragment
中有一个 google map
.有趣的是地图加载了,但 logcat 显示了 NullPointerException
,我只在尝试更新地图上的相机时才注意到它,以便它可以自动放大 marker代码>.只有这样地图才会崩溃.否则,地图在
fragment
中加载良好,但没有 marker
(即使我使用当前的 gps location
添加一个).
I have a google map
in a fragment
in my application. Funny thing is that the map loads, but the logcat displays a NullPointerException
, and I only noticed it when tried to update the camera on the map so that it could automatically zoom in on the marker
. Only then the map will crash. Otherwise, the map loads fine in the fragment
but with no marker
(even though I am adding one using my current gps location
).
编辑*
我知道它崩溃是因为 googleMap
为 null,但我不明白为什么它返回 null.地图渲染得很好,我以前做过这个,它通常在那个时候崩溃.不渲染.
I understand that its crashing because the googleMap
is null, but I don't understand why it's returning null. The map renders and well I've done this before and it usually crashes at that point. Doesn't render.
这里是 logcat 错误
Here is the logcat error
05-08 01:30:33.143 30130-30130/com.example.javed_000.famjam E/mapApp﹕ 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
05-08 01:30:33.152 30130-30130/com.example.javed_000.famjam E/MapFragment﹕ lat = 10.7270913 and Long = -61.5544451
05-08 01:30:33.155 30130-30130/com.example.javed_000.famjam D/AndroidRuntime﹕ Shutting down VM
05-08 01:30:33.156 30130-30130/com.example.javed_000.famjam E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.javed_000.famjam, PID: 30130
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.animateCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference
at com.example.javed_000.famjam.MapFragment.getLocation(MapFragment.java:60)
at com.example.javed_000.famjam.MapFragment.onCreateView(MapFragment.java:33)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
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.setCurrentItemInternal(ViewPager.java:555)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495)
at com.example.javed_000.famjam.SlidingTabLayout$TabClickListener.onClick(SlidingTabLayout.java:283)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
这是我的清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.javed_000.famjam" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAhgnZ7ESL-EIRS4Da_hGw_7u9IQI3SZE0" />
<activity
android:name=".SignInActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
这是我在gradle文件中的依赖
This is my dependencies in the gradle file
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.google.android.gms:play-services:7.0.0'
compile 'com.google.android.gms:play-services-maps:7.0.0'
}
这是我的布局文件.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/mapView"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
这是我从地图片段中提取的代码片段
Here is my code snippet from the Map Fragment
private void createMapView(){
try {
if(googleMap == null){
SupportMapFragment smf = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapView);
googleMap = smf.getMap();
if(googleMap == null) {
Toast.makeText(getActivity().getBaseContext(),
"Error creating map", Toast.LENGTH_SHORT).show();
}
}
} catch (NullPointerException exception){
Log.e("mapApp", exception.toString());
}
}
private void getLocation(){
gps = new GPSTracker(this.getActivity());
if(gps.CanGetLocation()){
latitude = gps.getLatitude();
longitude = gps.getLongitude();
Log.e("MapFragment","lat = " + latitude + " and Long = " + longitude);
addMarker(latitude,longitude);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,longitude), 12));
} else {
gps.showSettingsAlert();
}
}
private void addMarker(double lat, double lng){
if(googleMap != null){
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("Marker")
.draggable(true)
);
}
}
推荐答案
googleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
这对我有用.getSupportFragmentManager()
似乎是导致所有崩溃的原因.
This worked for me. getSupportFragmentManager()
seemed to be causing all the crashes.
这篇关于谷歌地图返回空指针异常谷歌地图Android V2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!