我正在尝试通过Fragment打开地图,由于某种原因,该应用会立即崩溃 我还检查了API是否正确,似乎还可以。 我已经在线检查了解决方案,但没有成功。 在MainActivity类中,我仅添加使MapFragment对象成为一行的行 谢谢你的帮助
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.avrahamzilberblat.battleshipfinal">
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".gameActivity" />
<activity android:name=".GameOver"></activity>
<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="AIzaSyCxgyRreLmjnrbHVQ9-Ka-_RS9gbTu_xLo" />
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
</application>
<!-- <meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@strings/google_map_api_key"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />-->
<!-- Location if you will use this -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.example.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
MainActivity的呼叫
getSupportFragmentManager().beginTransaction().replace(R.id.viewside,new MapFragment()).commit();
public class MapFragment extends Fragment implements OnMapReadyCallback {
GoogleMap mGoogleMap;
MapView mMapView;
View mView;
public MapFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mView=inflater.inflate(R.layout.fragment_map, container, false);
return mView;
}
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mMapView=(MapView) mView.findViewById(R.id.map);
if(mMapView!=null)
{
mMapView.onCreate(null);
mMapView.onResume();
mMapView.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getContext());
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
LatLng sydney=new LatLng(30,30);
mGoogleMap.addMarker(new MarkerOptions().position(sydney));
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
Xml与片段有关
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".MapFragment">
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"/>
</FrameLayout>
错误日志
2019-01-06 22:09:28.462 28924-28924/? E/id.defcontaine: Not starting debugger since process cannot load the jdwp agent.
--------- beginning of system
2019-01-06 22:09:28.735 1593-11870/? E/AudioFlinger: not enough memory for AudioTrack size=131296
2019-01-06 22:09:28.736 1593-11870/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
2019-01-06 22:09:28.737 2462-26517/? E/IAudioFlinger: createRecord returned error -12
2019-01-06 22:09:28.738 2462-26517/? E/AudioRecord: AudioFlinger could not create record track, status: -12
2019-01-06 22:09:28.738 2462-26517/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
2019-01-06 22:09:28.740 2462-26517/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
2019-01-06 22:09:28.742 2462-26517/? E/ActivityThread: Failed to find provider info for com.google.android.apps.gsa.testing.ui.audio.recorded
2019-01-06 22:09:28.801 1688-1907/? E/installd: Failed to delete /data/app/vmdl262505887.tmp: No such file or directory
2019-01-06 22:09:28.954 28950-28950/? E/ndroid.keychai: Not starting debugger since process cannot load the jdwp agent.
2019-01-06 22:09:30.426 1683-1707/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
2019-01-06 22:09:30.572 1683-1955/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
2019-01-06 22:09:33.390 29044-29044/? E/e.process.gapp: Not starting debugger since process cannot load the jdwp agent.
2019-01-06 22:09:33.800 29010-29010/com.avrahamzilberblat.battleshipfinal E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.avrahamzilberblat.battleshipfinal, PID: 29010
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setMapType(int)' on a null object reference
at com.avrahamzilberblat.battleshipfinal.MapFragment.onMapReady(MapFragment.java:59)
at com.google.android.gms.maps.zzac.zza(Unknown Source:7)
at com.google.android.gms.maps.internal.zzaq.dispatchTransaction(Unknown Source:31)
at com.google.android.gms.internal.maps.zzb.onTransact(Unknown Source:22)
at android.os.Binder.transact(Binder.java:667)
at fp.b(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):15)
at com.google.android.gms.maps.internal.bg.a(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):4)
at com.google.maps.api.android.lib6.impl.bj.run(:com.google.android.gms.dynamite_mapsdynamite@14799084@14.7.99 (100700-223214910):4)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-01-06 22:09:33.911 1838-1911/? E/InputDispatcher: channel 'd10f057 com.avrahamzilberblat.battleshipfinal/com.avrahamzilberblat.battleshipfinal.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-01-06 22:09:34.572 1593-11870/? E/AudioFlinger: not enough memory for AudioTrack size=131296
2019-01-06 22:09:34.591 1593-11870/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
2019-01-06 22:09:34.605 2462-29038/? E/IAudioFlinger: createRecord returned error -12
2019-01-06 22:09:34.606 2462-29038/? E/AudioRecord: AudioFlinger could not create record track, status: -12
2019-01-06 22:09:34.606 2462-29038/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
2019-01-06 22:09:34.607 2462-29038/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
2019-01-06 22:09:34.615 2462-29038/? E/ActivityThread: Failed to find provider info for com.google.android.apps.gsa.testing.ui.audio.recorded
2019-01-06 22:09:34.677 1774-1774/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2019-01-06 22:09:34.677 1774-1774/? E/netmgr: WifiForwarder unable to open QEMU pipe: Invalid argument
2019-01-06 22:09:34.847 2454-2454/? E/BeaconBle: Missing BluetoothAdapter
2019-01-06 22:09:34.919 2454-2454/? E/BeaconBle: Scan couldn't start for Places
2019-01-06 22:09:35.320 2454-2995/? E/memtrack: Couldn't load memtrack module
2019-01-06 22:09:37.602 2454-2936/? E/Volley: [130] BasicNetwork.performRequest: Unexpected response code 403 for https://www.googleapis.com/placesandroid/v1/getPlaceById?key=AIzaSyAf4nrRiEKvqzlRKTncQaAXMzb3ePYHr8Y
2019-01-06 22:09:37.613 2454-7936/? E/AsyncOperation: serviceID=65, operation=GetPlaceById
OperationException[Status{statusCode=ERROR, resolution=null}]
at bcmj.b(:com.google.android.gms@14799040@14.7.99 (100700-223214910):3)
at bcmc.a(:com.google.android.gms@14799040@14.7.99 (100700-223214910):29)
at ybc.run(:com.google.android.gms@14799040@14.7.99 (100700-223214910):27)
at beyi.run(:com.google.android.gms@14799040@14.7.99 (100700-223214910):2)
at qwc.run(:com.google.android.gms@14799040@14.7.99 (100700-223214910):21)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at rbx.run(Unknown Source:7)
at java.lang.Thread.run(Thread.java:764)
2019-01-06 22:09:38.537 1696-1754/? E/storaged: getDiskStats failed with result NOT_SUPPORTED and size 0
2019-01-06 22:09:39.701 1593-2147/? E/AudioFlinger: not enough memory for AudioTrack size=131296
2019-01-06 22:09:39.701 1593-2147/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
2019-01-06 22:09:39.710 2462-29038/? E/IAudioFlinger: createRecord returned error -12
2019-01-06 22:09:39.710 2462-29038/? E/AudioRecord: AudioFlinger could not create record track, status: -12
2019-01-06 22:09:39.712 2462-29038/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
2019-01-06 22:09:39.712 2462-29038/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
2019-01-06 22:09:39.714 2462-29038/? E/ActivityThread: Failed to find provider info for com.google.android.apps.gsa.testing.ui.audio.recorded
2019-01-06 22:09:41.401 1838-1852/? E/memtrack: Couldn't load memtrack module
2019-01-06 22:09:43.411 1838-1852/? E/memtrack: Couldn't load memtrack module
2019-01-06 22:09:44.757 1593-11870/? E/AudioFlinger: not enough memory for AudioTrack size=131296
2019-01-06 22:09:44.757 1593-11870/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
2019-01-06 22:09:44.763 2462-29038/? E/IAudioFlinger: createRecord returned error -12
2019-01-06 22:09:44.763 2462-29038/? E/AudioRecord: AudioFlinger could not create record track, status: -12
2019-01-06 22:09:44.763 2462-29038/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
2019-01-06 22:09:44.764 2462-29038/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
答案 0 :(得分:0)
您的“ mGoogleMap”变量为null,请按照以下代码进行修复:
@Override
public void onMapReady(final GoogleMap googleMap) {
mGoogleMap= googleMap;
//inside onMapReady give your "mGoogleMap" value and call mapConfig
mapConfig(googleMap);
}
private void mapConfig(GoogleMap googleMap) {
//this is a method for configuring your map(for clean code)
googleMap.addMarker(new MarkerOptions().position(sydney));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}