Mapview 给出 nullpointerexception 片段

Posted

技术标签:

【中文标题】Mapview 给出 nullpointerexception 片段【英文标题】:Mapview gives nullpointerexception fragment 【发布时间】:2016-04-18 07:38:48 【问题描述】:

当我尝试这样做时遇到空指针异常:

mapview.onCreate(savedInstanceState);

这是我的代码:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    rootView = inflater.inflate(R.layout.home_fragment, container, false);
    setRetainInstance(true);
    setHasOptionsMenu(true);

    try 
        MapsInitializer.initialize(getActivity().getApplicationContext());
     catch (Exception e) 
        e.printStackTrace();
    

    mapView = (MapView) rootView.findViewById(R.id.map);
    mapView.onCreate(savedInstanceState);
    mapView.onResume();
    .........

LOGCAT:

01-14 11:36:40.744 27070-27070/? E/androidRuntime: FATAL EXCEPTION: main
                                               java.lang.RuntimeException: Unable to start activity ComponentInfocom.test.maptest/com.test.maptest.MainActivity: java.lang.NullPointerException
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
                                                   at android.app.ActivityThread.access$600(ActivityThread.java:162)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
                                                   at android.os.Handler.dispatchMessage(Handler.java:107)
                                                   at android.os.Looper.loop(Looper.java:194)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5403)
                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:525)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
                                                   at dalvik.system.NativeStart.main(Native Method)
                                                Caused by: java.lang.NullPointerException
                                                   at com.google.maps.api.android.lib6.gmm6.m.ad.a(Unknown Source)
                                                   at com.google.maps.api.android.lib6.gmm6.c.h.a(Unknown Source)
                                                   at com.google.maps.api.android.lib6.gmm6.c.y.a(Unknown Source)
                                                   at com.google.maps.api.android.lib6.e.bd.a(Unknown Source)
                                                   at com.google.maps.api.android.lib6.e.ev.a(Unknown Source)
                                                   at com.google.maps.api.android.lib6.e.af.a(Unknown Source)
                                                   at com.google.android.gms.maps.internal.w.onTransact(SourceFile:66)
                                                   at android.os.Binder.transact(Binder.java:310)
                                                   at com.google.android.gms.maps.internal.IMapViewDelegate$zza$zza.onCreate(Unknown Source)
                                                   at com.google.android.gms.maps.MapView$zza.onCreate(Unknown Source)
                                                   at com.google.android.gms.dynamic.zza$3.zzb(Unknown Source)
                                                   at com.google.android.gms.dynamic.zza$1.zza(Unknown Source)
                                                   at com.google.android.gms.maps.MapView$zzb.zzzW(Unknown Source)
                                                   at com.google.android.gms.maps.MapView$zzb.zza(Unknown Source)
                                                   at com.google.android.gms.dynamic.zza.zza(Unknown Source)
                                                   at com.google.android.gms.dynamic.zza.onCreate(Unknown Source)
                                                   at com.google.android.gms.maps.MapView.onCreate(Unknown Source)
                                                   at com.test.maptest.TestFragment.onCreateView(TestFragment.java:343)
                                                   at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
                                                   at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2042)
                                                   at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:165)
                                                   at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:543)
                                                   at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1170)
                                                   at android.app.Activity.performStart(Activity.java:5132)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395) 
                                                   at android.app.ActivityThread.access$600(ActivityThread.java:162) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:107) 
                                                   at android.os.Looper.loop(Looper.java:194) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5403) 
                                                   at java.lang.reflect.Method.invokeNative(Native Method) 
                                                   at java.lang.reflect.Method.invoke(Method.java:525) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
                                                   at dalvik.system.NativeStart.main(Native Method)

依赖:

dependencies 
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.google.android.gms:play-services:8.4.0'
compile 'com.mcxiaoke.volley:library:1.0.16'
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.google.android.gms:play-services-ads:8.4.0'
compile 'com.google.android.gms:play-services-identity:8.4.0'
compile 'com.google.android.gms:play-services-gcm:8.4.0'

问题是我只能在一部 4.2.2 的手机中使用它,其他所有不同版本的手机都可以使用。地图已显示,一切正常。 谁能帮我弄清楚这是怎么回事? 谢谢。

编辑:这款特定手机几个月前曾使用此代码,所以问题可能出在较新的 google play 服务和支持库上? 如果是这样,我们该如何纠正它?

【问题讨论】:

@Ms Yvette 添加了 logcat 和依赖项 【参考方案1】:

因为你打过电话

mapView.onCreate(savedInstanceState);
mapView.onResume();

这些方法不需要你调用。它们将由 Android 本身执行。正确遵循 mapview 教程。 :)

同样不要做onCreateView的东西,做onViewCreated的fragment的方法。请发布您正在初始化的 xml 文件。

How to use MapView in android using google map V2?

https://github.com/googlemaps/android-samples/blob/master/ApiDemos%2Fapp%2Fsrc%2Fmain%2Fjava%2Fcom%2Fexample%2Fmapdemo%2FBasicMapDemoActivity.java

【讨论】:

正确的教程在哪里?为什么它可以在其他几款手机上运行? 尽管如此用法错误,但它必须在某个时候中断。如果重新初始化,它可能会中断。 提供的链接是我正在做的。哪里用错了?使用 Google Play Services 8.4.0,如依赖项所示 还有 mapview.onResume() 只是为了立即显示地图。如果我删除它,它仍然会给出 nullpointerexception .....

以上是关于Mapview 给出 nullpointerexception 片段的主要内容,如果未能解决你的问题,请参考以下文章

类型“MapView”上不存在属性“地图”

内部编译器错误:org.eclipse.jdt.internal.compiler.problem.ProblemHandler.handle 处的 java.lang.NullPointerExce

亲測,Eclipse报"An error has occurred,See error log for more details. java.lang.NullPointerExce

亲測,Eclipse报"An error has occurred,See error log for more details. java.lang.NullPointerExce

反应原生 - eslint 给出太多错误

Software Testing Homework 2