歌地图返回NullPointerException异常谷歌地图的

歌地图返回NullPointerException异常谷歌地图的

本文介绍了谷歌地图返回NullPointerException异常谷歌地图的Andr​​oid V2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中的片段的谷歌地图。有趣的是,地图加载,但logcat中会显示一个NullPointerException异常,而我只注意到它时,试图更新相机在地图上,以便它可以自动放大的标记。只有这样,地图会崩溃。否则,地图加载罚款的片段,但没有标记(即使我用我的当前GPS位置添加一个)。

EDITED *


据我所知,它的崩溃,因为GoogleMap的是空的,但我不明白为什么它返回null。地图渲染和好,我之前已经做到了这一点,它通常会崩溃在这一点上。不渲染。


下面是错误的logcat

一月五日至八日:30:33.143 30130-30130 / com.example.javed_000.famjam E / mapApp:显示java.lang.NullPointerException:尝试调用虚拟方法com.google.android.gms.maps.GoogleMap com.google。在一个空对象引用android.gms.maps.SupportMapFragment.getMap()
一月五日至八日:30:33.152 30130-30130 / com.example.javed_000.famjam E / MapFragment:土地增值税= 10.7270913和长= -61.5544451
一月五日至八日:30:33.155 30130-30130 / com.example.javed_000.famjam D / AndroidRuntime:关闭虚拟机
一月五日至八日:30:33.156 30130-30130 / com.example.javed_000.famjam E / AndroidRuntime:致命异常:主要
    工艺:com.example.javed_000.famjam,PID:30130
    显示java.lang.NullPointerException:尝试在空对象引用调用虚拟方法无效com.google.android.gms.maps.GoogleMap.animateCamera(com.google.android.gms.maps.CameraUpdate)'
            在com.example.javed_000.famjam.MapFragment.getLocation(MapFragment.java:60)
            在com.example.javed_000.famjam.MapFragment.onCreateView(MapFragment.java:33)
            在android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
            在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
            在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
            在android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:488)
            在android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
            在android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
            在android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:555)
            在android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
            在android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495)
            在com.example.javed_000.famjam.SlidingTabLayout $ TabClickListener.onClick(SlidingTabLayout.java:283)
            在android.view.View.performClick(View.java:4780)
            在android.view.View $ PerformClick.run(View.java:19866)
            在android.os.Handler.handleCallback(Handler.java:739)
            在android.os.Handler.dispatchMessage(Handler.java:95)
            在android.os.Looper.loop(Looper.java:135)
            在android.app.ActivityThread.main(ActivityThread.java:5254)
            在java.lang.reflect.Method.invoke(本机方法)
            在java.lang.reflect.Method.invoke(Method.java:372)
            在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)
            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

这是我的清单文件
    

 <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
<使用-权限的Andr​​oid:名称=android.permission.GET_ACCOUNTS/>
<使用-权限的Andr​​oid:名称=android.permission.USE_CREDENTIALS/>
<使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE/>
<使用-权限的Andr​​oid:名称=com.google.android.providers.gsf.permission.READ_GSERVICES/>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_COARSE_LOCATION/>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_FINE_LOCATION/>


<应用
    机器人:allowBackup =真
    机器人:图标=@可绘制/发射器
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ AppTheme>

    <元数据的android:NAME =com.google.android.gms.version机器人:值=@整数/ google_play_services_version/>

    &所述;元数据
        机器人:名称=com.google.android.maps.v2.API_KEY
        机器人:值=AIzaSyAhgnZ7ESL-EIRS4Da_hGw_7u9IQI3SZE0/>

    <活动
        机器人:名称=。SignInActivity
        机器人:标签=@字符串/ APP_NAME>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.MAIN/>

            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
    <活动
        机器人:名称=。MainActivity
        机器人:标签=@字符串/ APP_NAME>
    < /活性GT;
< /用途>
 

这是我在摇篮文件相关

 相关性{
编译文件树(导演:库,包括:['的* .jar'])
编译com.android.support:appcompat-v7:22.0.0
编译com.google.android.gms:播放服务:7.0.0
编译com.google.android.gms:播放服务,地图:7.0.0
 

}

这是我的布局文件。

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent>

<片段
    机器人:ID =@ + ID /图形页面
    机器人:名称=com.google.android.gms.maps.SupportMapFragment
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent/>

< / RelativeLayout的>
 

下面是从地图片段

我的code段

 私人无效createMapView(){
    尝试 {
        如果(GoogleMap的== NULL){
            。SupportMapFragment SMF =(SupportMapFragment)getActivity()getSupportFragmentManager()findFragmentById(R.id.mapView)。
            GoogleMap的= smf.getMap();

            如果(GoogleMap的== NULL){
                Toast.makeText(getActivity()。getBaseContext(),
                        错误创建地图,Toast.LENGTH_SHORT).show();
            }
        }
    }赶上(NullPointerException异常除外){
        Log.e(mapApp,exception.toString());
    }
}

私人无效的getLocation(){
    GPS =新GPSTracker(this.getActivity());
    如果(gps.CanGetLocation()){
        纬度= gps.getLatitude();
        经度= gps.getLongitude();
        Log.e(MapFragment,土地增值税=+纬度+和长=+经度);
        addMarker(纬度,经度);
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(新经纬度(纬度,经度),12));
    } 其他 {
        gps.showSettingsAlert();
    }
}

私人无效addMarker(双纬度,双LNG){
    如果(GoogleMap的!= NULL){
        googleMap.addMarker(新MarkerOptions()
                        .position(新经纬度(纬度,经度))
                        .title伪(标记)
                        .draggable(真)
        );
    }
}
 

解决方案

 的GoogleMap =((SupportMapFragment)getChildFragmentManager()findFragmentById(R.id.map)。)的GetMap()。
 

这为我工作。 getSupportFragmentManager()似乎是导致所有的崩溃。

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).

EDITED*


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.


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)

This is my manifest file

<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>

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'

}

This is my layout file.

<?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();

This worked for me. getSupportFragmentManager() seemed to be causing all the crashes.

这篇关于谷歌地图返回NullPointerException异常谷歌地图的Andr​​oid V2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:28