(日食)无法显示标记/空指针异常谷歌地图 API 安卓



大家下午好,

在开始Android开发之前,我在设置所有内容时遇到了多个问题,现在已经解决了。

然而,在遵循了谷歌关于如何在Android设备上设置谷歌地图的教程之后,我慢慢开始放弃......

我完成了整个创建地图 -> 映射对象,直到我开始收到有关我的 getMapAsync(this( 和我的 fragmentManager 的空指针异常,所以我决定改用 googlemap 方法。

但是,即使我能够正常显示我的地图,我现在也无法在地图上显示标记。

我希望有人快速查看我的代码,看看他们是否可以发现任何问题。我已经浏览了SO,发现了有关此主题的多个问题,但没有一个真正帮助了我。

这是我的班级:

package com.example.birdview;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
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.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class Startup extends FragmentActivity 
implements OnMapReadyCallback {
    /*
     * Private methods:
     * 
     */
    private void setUpMap(){
        try {
             if (googleMap == null) {
                googleMap = ((MapFragment) getFragmentManager().
                findFragmentById(R.id.map)).getMap();
             }
             googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
          }
          catch (Exception e) {
             e.printStackTrace();
          }
    }
    /*
     * Private variables:
     */
    double latitude = 13.094192 ;
    double longitude = 80.121689;
    private GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_startup);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
        }
        setUpMap();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.startup, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        public PlaceholderFragment() {
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_startup, container, false);
            return rootView;
        }
    }
    @Override
    public void onMapReady(GoogleMap map) {
        Marker hamburg = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude))
                  .title("Hello Maps"));
        // TODO Auto-generated method stub
    }

}

fragment_startup.xml:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.birdview.Startup$PlaceholderFragment" >
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<fragment
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

activity_startup:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.birdview.Startup"
tools:ignore="MergeRootFrame" />

birdview_manifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.birdview"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />
    <!--  Set application permissions: -->
    <permission
      android:name="com.example.birdview.permission.MAPS_RECEIVE"
      android:protectionLevel="signature" />
        <uses-permission android:name="your.package.name.permission.MAPS_RECEIVE"/> 
        <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" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

   <!--  Set openGL permissions: -->
   <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 for google maps api credentials: -->
        <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyAbIbXmufKtMn6t72Ya4oipOK1iowIEPYY"/>
        <meta-data 
             android:name="com.google.android.gms.version"
             android:value="@integer/google_play_services_version" />

        <activity
            android:name=".Startup"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

如前所述,我首先在使用谷歌(谷歌地图 api(提供的实现时获得空指针异常。更改为当前实现后,我现在能够显示地图,没有标记实例......

这是日志猫:

02-15 12:56:12.240: D/ViewRootImpl(17859): ViewPostImeInputStage ACTION_DOWN
02-15 12:56:14.262: V/ActivityThread(17859): updateVisibility : ActivityRecord{4a02b89 token=android.os.BinderProxy@35c8e20e {com.example.birdview/com.example.birdview.Startup}} show : false
02-15 12:56:21.619: D/ResourcesManager(20806): creating new AssetManager and set to /data/app/com.example.birdview-1/base.apk
02-15 12:56:21.709: W/ResourceType(20806): For resource 0x7f02007c, entry index(124) is beyond type entryCount(56)
02-15 12:56:21.709: W/ResourceType(20806): Failure getting entry for 0x7f02007c (t=1 e=124) (error -75)
02-15 12:56:21.719: E/Resources(20806): RunTimeException
02-15 12:56:21.719: E/Resources(20806): android.content.res.Resources$NotFoundException: Resource ID #0x7f02007c
02-15 12:56:21.719: E/Resources(20806):     at android.content.res.Resources.getValue(Resources.java:2345)
02-15 12:56:21.719: E/Resources(20806):     at android.content.res.Resources.startRC(Resources.java:1059)
02-15 12:56:21.719: E/Resources(20806):     at android.app.ActivityThread$mRunnable.run(ActivityThread.java:2508)
02-15 12:56:21.719: E/Resources(20806):     at java.lang.Thread.run(Thread.java:818)
02-15 12:56:21.759: W/System.err(20806): java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.MapFragment.getMap()' on a null object reference
02-15 12:56:21.759: W/System.err(20806):    at com.example.birdview.Startup.setUpMap(Startup.java:31)
02-15 12:56:21.759: W/System.err(20806):    at com.example.birdview.Startup.onCreate(Startup.java:57)
02-15 12:56:21.759: W/System.err(20806):    at android.app.Activity.performCreate(Activity.java:6289)
02-15 12:56:21.759: W/System.err(20806):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
02-15 12:56:21.759: W/System.err(20806):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
02-15 12:56:21.759: W/System.err(20806):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
02-15 12:56:21.759: W/System.err(20806):    at android.app.ActivityThread.access$900(ActivityThread.java:177)
02-15 12:56:21.759: W/System.err(20806):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
02-15 12:56:21.759: W/System.err(20806):    at android.os.Handler.dispatchMessage(Handler.java:102)
02-15 12:56:21.759: W/System.err(20806):    at android.os.Looper.loop(Looper.java:145)
02-15 12:56:21.759: W/System.err(20806):    at android.app.ActivityThread.main(ActivityThread.java:5942)
02-15 12:56:21.759: W/System.err(20806):    at java.lang.reflect.Method.invoke(Native Method)
02-15 12:56:21.759: W/System.err(20806):    at java.lang.reflect.Method.invoke(Method.java:372)
02-15 12:56:21.759: W/System.err(20806):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
02-15 12:56:21.759: W/System.err(20806):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
02-15 12:56:21.759: D/Activity(20806): performCreate Call secproduct feature valuefalse
02-15 12:56:21.759: D/Activity(20806): performCreate Call debug elastic valuetrue
02-15 12:56:21.769: I/zzad(20806): Making Creator dynamically
02-15 12:56:21.769: D/ResourcesManager(20806): creating new AssetManager and set to /data/app/com.google.android.gms-2/base.apk
02-15 12:56:21.780: W/ResourcesManager(20806): Asset path '/system/framework/com.android.media.remotedisplay.jar' does not exist or contains no resources.
02-15 12:56:21.780: W/ResourcesManager(20806): Asset path '/system/framework/com.android.location.provider.jar' does not exist or contains no resources.
02-15 12:56:21.910: D/ChimeraCfgMgr(20806): Reading stored module config
02-15 12:56:21.940: D/ChimeraCfgMgr(20806): Loading module com.google.android.gms.maps from APK /data/data/com.google.android.gms/app_chimera/chimera-module-root/module-71c764a6f3cb92bdc5525a965b589e7c5ed304f3/MapsModule.apk
02-15 12:56:21.950: D/ChimeraModuleLdr(20806): Loading module APK /data/data/com.google.android.gms/app_chimera/chimera-module-root/module-71c764a6f3cb92bdc5525a965b589e7c5ed304f3/MapsModule.apk
02-15 12:56:21.950: D/ChimeraFileApk(20806): Primary ABI of requesting process is armeabi-v7a
02-15 12:56:21.950: I/art(20806): DexFile_isDexOptNeeded failed to open oat file '/data/dalvik-cache/arm/data@data@com.google.android.gms@app_chimera@chimera-module-root@module-71c764a6f3cb92bdc5525a965b589e7c5ed304f3@MapsModule.apk@classes.dex' for file location '/data/data/com.google.android.gms/app_chimera/chimera-module-root/module-71c764a6f3cb92bdc5525a965b589e7c5ed304f3/MapsModule.apk': Failed to open oat filename for reading: No such file or directory
02-15 12:56:21.960: D/ChimeraFileApk(20806): Classloading successful. Optimized code found.
02-15 12:56:22.040: I/Google Maps Android API(20806): Google Play services client version: 8487000
02-15 12:56:22.050: I/Google Maps Android API(20806): Google Play services package version: 8489238
02-15 12:56:22.220: I/e(20806): Token loaded from file. Expires in: 419712815 ms.
02-15 12:56:22.220: I/e(20806): Scheduling next attempt in 419412 seconds.
02-15 12:56:22.350: D/AbsListView(20806): Get MotionRecognitionManager
02-15 12:56:22.420: W/ContextImpl(20806): Failed to ensure directory: /storage/extSdCard/Android/data/com.example.birdview/cache
02-15 12:56:22.450: W/ActivityThread(20806): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
02-15 12:56:22.460: I/System.out(20806): (HTTPLog)-Static: isSBSettingEnabled false
02-15 12:56:22.460: I/System.out(20806): (HTTPLog)-Static: isShipBuild true
02-15 12:56:22.460: I/System.out(20806): (HTTPLog)-Thread-88543-829859576: SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false
02-15 12:56:22.460: I/System.out(20806): (HTTPLog)-Static: isSBSettingEnabled false
02-15 12:56:22.540: I/System.out(20806): KnoxVpnUidStorageknoxVpnSupported API value returned is false
02-15 12:56:22.570: D/OpenGLRenderer(20806): Render dirty regions requested: true
02-15 12:56:22.610: I/Adreno-EGL(20806): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
02-15 12:56:22.610: I/Adreno-EGL(20806): OpenGL ES Shader Compiler Version: E031.25.03.06
02-15 12:56:22.610: I/Adreno-EGL(20806): Build Date: 01/24/15 Sat
02-15 12:56:22.610: I/Adreno-EGL(20806): Local Branch: AF11_RB1_AU15
02-15 12:56:22.610: I/Adreno-EGL(20806): Remote Branch: 
02-15 12:56:22.610: I/Adreno-EGL(20806): Local Patches: 
02-15 12:56:22.610: I/Adreno-EGL(20806): Reconstruct Branch: 
02-15 12:56:22.620: I/OpenGLRenderer(20806): Initialized EGL, version 1.4
02-15 12:56:22.660: D/OpenGLRenderer(20806): Enabling debug mode 0
02-15 12:56:22.780: I/Timeline(20806): Timeline: Activity_idle id: android.os.BinderProxy@2135111a time:286565164
02-15 12:56:23.061: I/art(20806): Background sticky concurrent mark sweep GC freed 33313(2MB) AllocSpace objects, 42(7MB) LOS objects, 12% free, 22MB/25MB, paused 14.770ms total 113.891ms
02-15 12:56:23.511: I/art(20806): Background partial concurrent mark sweep GC freed 1231(40KB) AllocSpace objects, 13(21MB) LOS objects, 37% free, 26MB/42MB, paused 7.263ms total 83.862ms
02-15 12:56:23.731: I/System.out(20806): (HTTPLog)-Static: isSBSettingEnabled false
02-15 12:56:23.852: I/System.out(20806): (HTTPLog)-Static: isSBSettingEnabled false
02-15 12:56:27.535: I/System.out(20806): KnoxVpnUidStorageknoxVpnSupported API value returned is false
02-15 12:56:32.510: D/Volley(20806): [88545] a.a: HTTP response for request=<[ ] https://csi.gstatic.com/csi?s=maps_android_api&v=3&action=map_start_up&it=map_load.1513,on_create.1,on_resume.1,init.323&irt=1513,324,327,323 0x4d844933 NORMAL 2> [lifetime=8773], [size=0], [rc=204], [retryCount=0]
02-15 12:56:32.540: D/Volley(20806): [1] p.b: 8812 ms: [ ] https://csi.gstatic.com/csi?s=maps_android_api&v=3&action=map_start_up&it=map_load.1513,on_create.1,on_resume.1,init.323&irt=1513,324,327,323 0x4d844933 NORMAL 2

您的代码有两个问题:

首先,这一行:

googleMap=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

将始终返回 null,因为在布局活动中,您没有带有 id 的片段map它是您的PlaceholderFragment片段具有 id map MapFragment。您需要使用getChildFragmentManager方法。

第二个问题,您的方法onMapReady永远不会被调用,因为您没有使用getMapAsync(this)方法在MapFragment上注册。

我对你的问题的建议:

将您的activity_startup更改为以下内容:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="kz.ibragimov.googlemaps.MapsActivity" />

您的活动Startup

package com.example.birdview;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
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 Startup extends FragmentActivity implements OnMapReadyCallback {
    private GoogleMap mMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_startup);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        double latitude = 13.094192;
        double longitude = 80.121689;
        mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps"));
    }
}

最新更新