gpt4 book ai didi

Android setUpMapIfNeeded();给出空指针异常

转载 作者:行者123 更新时间:2023-11-29 20:55:06 24 4
gpt4 key购买 nike

所以我刚刚创建了一个新的谷歌地图 Activity ,如果我尝试运行我的应用程序,它会给我一个空指针异常

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setUpMapIfNeeded();
}

@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}

private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}

private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}

这是我的布局

<fragment 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"
android:id="@+id/map"
tools:context="android.ehb.be.lab4sqlliteex.MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>

它只是在我添加 Activity 时由 android studio 自动生成的。我的类(class)扩展了 FragmentActivity

这是我的 list 文件

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<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="@string/google_maps_key"/>


<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />

<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBxMzLA6jjVtPQEHOrOPTRzKnjPu6bKKGA"
/>
<activity
android:name="android.ehb.be.lab4sqlliteex.MapsActivity"
android:label="@string/title_activity_maps">
</activity>
<activity
android:name=".BucketListActivity"
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=".AlbumsActivity"
android:label="Albums"
android:parentActivityName=".BucketListActivity">

</activity>
</application>

有人知道如何解决这个问题吗?

这是我得到的错误:

  java.lang.RuntimeException: Unable to start activity ComponentInfo{android.ehb.be.lab4sqlliteex/android.ehb.be.lab4sqlliteex.MapsActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5867)
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:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.ehb.be.lab4sqlliteex.MapsActivity.setUpMapIfNeeded(MapsActivity.java:46)
at android.ehb.be.lab4sqlliteex.MapsActivity.onCreate(MapsActivity.java:18)

最佳答案

需要在onCreate()中调用:setContentView(R.layout.main_layout)。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
setUpMapIfNeeded();
}

关于Android setUpMapIfNeeded();给出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27990239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com