问题描述
经过与我的头衔有关的SO问题后,我找不到解决问题的办法。我在 mapFragment.getMapAsync
中有一个 NullPointerException
。以下是我的 MapActivity
代码。
package com.example.jerofad。我的位置;
导入android.app.FragmentManager;
导入android.support.v4.app.FragmentActivity;
导入android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
导入com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.MarkerOptions;
公共类MapsActivity扩展FragmentActivity实现OnMapReadyCallback {
私有GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
//获取SupportMapFragment并在地图准备好使用时得到通知。
SupportMapFragment mapFragment =(SupportMapFragment)this.getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap){
mMap = googleMap;
//在悉尼添加标记并移动相机
LatLng sydney = new LatLng(-34,151);
mMap.addMarker(new MarkerOptions()。position(sydney).title(Marker in Sydney));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mMap.getUiSettings()。setZoomControlsEnabled(true);
$ / code $ / pre
我使用 google play服务8.4.0
。
<?xml version =1.0encoding =utf-8?>我的Android Manifest在下面给出。
< manifest xmlns:android =http://schemas.android.com/apk/res/android
package =com.example.jerofad.mylocation>
使用
Google地图Android API v2时不需要ACCESS_COARSE / FINE_LOCATION权限,但您必须指定粗略或精细的
位置'MyLocation'功能的权限。
- >
< uses-permission android:name =android.permission.ACCESS_FINE_LOCATION/>
< uses-permission android:name =android.permission.ACCESS_COARSE_LOCATION/>
< uses-permission android:name =android.permission.ACCESS_NETWORK_STATE/>
< uses-permission android:name =android.permission.INTERNET/>
<使用权限android:name =com.google.android.providers.gsf.permission.READ_GSERVICES/>
<使用权限android:name =android.permission.WRITE_EXTERNAL_STORAGE
android:maxSdkVersion =22/>
< permission
android:name =com.example.jerofad.mylocation.permission.MAPS_RECEIVE
android:protectionLevel =signature/>
< uses-permission android:name =com.example.jerofad.mylocation.permission.MAPS_RECEIVE/>
< uses-permission
android:name =android.permission.READ_EXTERNAL_STORAGE
android:maxSdkVersion =22/>
< uses-feature
android:glEsVersion =0x00020000
android:required =true/>
$ application b $ b android:allowBackup =true
android:icon =@ drawable / mylocation
android:label =@ string / app_name
android:supportsRtl =true
android:theme =@ style / AppTheme>
< meta-data
android:name =com.google.android.geo.API_KEY
android:value =@ string / google_maps_key/>
< meta-data
android:name =com.google.android.gms.version
android:value =@ integer / google_play_services_version/>
< activity
android:name =。MapsActivity
android:label =@ string / title_activity_maps>
< / activity>
< activity
android:name =。LauncherActivity
android:label =@ string / title_activity_launcher
android:theme =@ style / AppTheme.NoActionBar> ;
< intent-filter>
< category android:name =android.intent.category.LAUNCHER/>
< / intent-filter>
< / activity>
< / application>
如何解决错误在 mapFragment.getMapAsync(this)
。提前致谢。
04-25 11:11:49.079 31448-31448 / com.example.jerofad.mylocation
E / AndroidRuntime:FATAL EXCEPTION:main
处理:com.example.jerofad.mylocation,PID:31448
java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.jerofad。 mylocation / com.example.jerofad.mylocation.MapsActivity}:java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity( Activity.Thread.java:2390)
at android.app.ActivityThread.access $ 800(ActivityThread.java:151)
at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1321)
在android.os.Handler.dispatchMessage(Handler.java:110)
在android.os.Looper.loop(Looper.java:193)
在android.app.ActivityThread.main(ActivityThread。 java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)$ b $ at java.lang.reflect.Met hod.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:825)
at com.android.internal.os.ZygoteInit。 main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)
引起:java.lang.NullPointerException
at com.example.jerofad.mylocation.MapsActivity .onCreate(MapsActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5423)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access $ 800(ActivityThread .java:151)
at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)$ and
id.app.ActivityThread.main(ActivityThread.java:5299)$ b $在java.lang.reflect.Method.invokeNative(本地方法)$ b $在java.lang.reflect.Method.invoke(Method.java :515)
at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641 )
at dalvik.system.NativeStart.main(Native Method)
这里是activity_maps
<?xml version =1.0encoding =utf-8?>
< fragment xmlns:android =http://schemas.android.com/apk/res/android
android:id =@ + id / map
class = com.google.android.gms.maps.MapFragment
android:layout_width =match_parent
android:layout_height =match_parent
android:name =com.example.jerofad.mylocation .MapsActivity/>
解决方案请试试这个,
删除/评论SupportMapfragment行一段时间,
试试这个,
MapFragment mapFragment =(MapFragment)getFragmentManager().findFragmentById(R.id.map);
After going through SO questions relating to my title, I couldn't find a solution to the problem. I am having a NullPointerException
at mapFragment.getMapAsync
. Below is my MapActivity
code.
package com.example.jerofad.mylocation;
import android.app.FragmentManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) this.getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mMap.getUiSettings().setZoomControlsEnabled(true);
}
}
I am using google play service 8.4.0
. My Android Manifest is given below.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jerofad.mylocation">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<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"
android:maxSdkVersion="22"/>
<permission
android:name="com.example.jerofad.mylocation.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.jerofad.mylocation.permission.MAPS_RECEIVE" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="22" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/mylocation"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
</activity>
<activity
android:name=".LauncherActivity"
android:label="@string/title_activity_launcher"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
How can I fix the error at mapFragment.getMapAsync(this)
. Thanks in advance. Here is the crash Log.
04-25 11:11:49.079 31448-31448/com.example.jerofad.mylocation
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jerofad.mylocation, PID: 31448
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jerofad.mylocation/com.example.jerofad.mylocation.MapsActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
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:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.jerofad.mylocation.MapsActivity.onCreate(MapsActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5423)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
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:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)
Here is the activity_maps.xml.
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
class ="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.jerofad.mylocation.MapsActivity" />
解决方案 Please try this,
Remove/comment SupportMapfragment line for a while and Try this,
MapFragment mapFragment = (MapFragment) getFragmentManager() .findFragmentById(R.id.map);
这篇关于NullPointerException在mapFragment.getMapAsync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!