在谷歌地图API第2版随机NullPointerException异常异常、地图、API、NullPointerException

2023-09-04 09:54:10 作者:风流又倜傥&

我一直在使用谷歌地图API第2版(3.2.25版(761454-30))在我的Andr​​oid应用程序,有时它完美的作品,虽然有时(这是每次得到更多的时候)我得到一个空指针异常。我没有做任何事情与地图太不一样了,它已经发生了一些的Nexus 4以及上一台Nexus S.我发现一个的问题上GMaps实现-API的问题的项目,虽然我没有从那里得到任何帮助。 I'mputting这里的问题,然后用一些细节,看看是否有人已经通过这个已经消失了。

正如我所说的,我什么也没做任何不同,因为他们提出的API,唯一的事情,我想我可能会做这不是每个人都一定不会(并可能导致一些问题,IDK)是

在我使用的是SupportMapFragment在另一个片段,又名作为一个嵌套的片段。我还使用的ActionBar福尔摩斯,让其他片段是一个SherlockFragment只是说。 在我实例化我的片段的onCreateView的SupportMapFragment(而不是把它放在XML),然后addind到一个的FrameLayout我的布局。 在我申请了一些Y平移地图(带nineoldanroids),以便我能获得的效果接近一个作为对Foursquare的应用程序(虽然我从来没有在Foursquare上的NPE)。 在我创建和清除一些标记为用户进步上的应用程序(即不应该是吧,因为这个错误发生在启动时,在加载framgent)

这是多少呢,我不会把code在这里,因为它不像它只是一个片段是无法正常工作,但我列出了很多我在做什么。这里是堆栈跟踪(它始终是实际上是相同的):

 显示java.lang.NullPointerException
在java.nio.ReadWriteDirectByteBuffer.put(ReadWriteDirectByteBuffer.java:137)
在java.nio.ShortToByteBufferAdapter.put(ShortToByteBufferAdapter.java:163)
在maps.z.d.d(来源不明)
在maps.z.d.a(来源不明)
在maps.aq.a.a(来源不明)
在maps.aq.ao.b(来源不明)
在maps.aq.ao.a(来源不明)
在maps.v.g.a(来源不明)
在maps.v.g.b(来源不明)
在maps.p.p.l(来源不明)
在maps.p.p.run(来源不明)
 

解决方案

嗯,我成功地解决这个问题。这实际上不是一个修复,但至少我回避我的应用程序崩溃时异常。我所做的就是覆盖默认的未捕获异常处理程序和SUP preSS崩溃的应用程序时,未捕获的异常是这一个。这次事故是由于一个NullPointerException异常的,它总是发生在一个GLThread(必须是从谷歌地图绘制code)。所以,我检查所有的prerequisites适用,如果是这样,我发送一个广播的自定义操作,所以我可以听上,我使用谷歌地图的片段上播出,取代我SupportMapFragment时,我得到一个。因为我们得到了一个未捕获的异常,GLThread中断和图纸到谷歌地图碎片被暂停,所以我们也需要更换谷歌地图碎片的地图重新工作。

谷歌地图界面将重新设计 在I O开发大会上推出

下面是code我不得不添加:

在我的应用程序的onCreate:

 最后的UncaughtExceptionHandler的DefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(新的UncaughtExceptionHandler(){
    @覆盖
    公共无效uncaughtException(线程的线程,可抛出前){
        如果(前的instanceof NullPointerException异常和放大器;&放大器;的Thread.getName()startsWith(GLThread)){
            对于(一个StackTraceElement的StackTraceElement:ex.getStackTrace()){
                如果(stackTraceElement.getClassName()。包括(图)){
                    sendBroadcast(新意图(ACTION_MAPS_NPE));
                    返回;
                }
            }
        }
        defaultHandler.uncaughtException(螺纹,前);
    }
});
 

然后在我的片段,它采用的是谷歌地图碎片,我加入SupportMapFragment对onCreateView布局之前正确注册该广播接收器(和注销它onDestroyView):

 私有类GoogleMapsNPEReceiver扩展的BroadcastReceiver {

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        片段mapFragment = getChildFragmentManager()findFragmentById(R.id.map_fragment_container)。
        如果(mapFragment!= NULL){
            getChildFragmentManager()的BeginTransaction()
                    .replace(R.id.map_fragment_container,SupportMapFragment.newInstance())
                    。承诺();
            mMapInitialized = FALSE;
            //我们不会有onResume到了初始化我们的地图。尝试在100ms后进行初始化。
            最终的处理程序处理程序=新的处理程序();
            handler.postDelayed(新的Runnable(){
                @覆盖
                公共无效的run(){
                    如果(getGoogleMap()== NULL){
                        handler.postDelayed(此,100);
                    }
                }
            },100);
        }
    }
}
 

希望这有助于任何人以同样的问题。如果任何人有实际修复它,请让我知道。

I've been working with Google Maps API v2 (version 3.2.25 (761454-30)) on my Android app, and sometimes it works perfectly though sometimes (which is getting each time more often) I'm getting a NullPointerException. I'm not doing anything too different with the map, and it has happened on some Nexus 4 as well on a Nexus S. I've found an issue on the gmaps-api-issues project though I don't get any help from there. I'mputting the question here then with some details to see if someone has gone through this already.

As I said, I'm not doing anything any different as what they suggest on the API, and the only things I think I may be doing which not everyone necessarily does (and that may be causing some problem, idk) is:

I'm using a SupportMapFragment inside another Fragment, a.k.a. as a nested fragment. I'm also using ActionBar Sherlock, so that other fragment is a SherlockFragment just to say. I'm instantiating the SupportMapFragment on my Fragment's onCreateView (instead of putting it on XML) and then addind it to a FrameLayout on my layout. I'm applying some Y translation to the map (with nineoldanroids), so that I can acquire an effect close to the one as on the Foursquare app (though I've never got an NPE on Foursquare). I'm creating and clearing some markers as the user progresses on the app (that shouldn't be it, since the bug happens on startup, while loading the framgent)

That is much it, I won't put the code here since it is not like it's just a snippet which is not working but I've listed much of what I'm doing. Here is the stack trace (it is always practically the same):

java.lang.NullPointerException
at java.nio.ReadWriteDirectByteBuffer.put(ReadWriteDirectByteBuffer.java:137)
at java.nio.ShortToByteBufferAdapter.put(ShortToByteBufferAdapter.java:163)
at maps.z.d.d(Unknown Source)
at maps.z.d.a(Unknown Source)
at maps.aq.a.a(Unknown Source)
at maps.aq.ao.b(Unknown Source)
at maps.aq.ao.a(Unknown Source)
at maps.v.g.a(Unknown Source)
at maps.v.g.b(Unknown Source)
at maps.p.p.l(Unknown Source)
at maps.p.p.run(Unknown Source)

解决方案

Well, I managed to "fix" this issue. It isn't actually a fix but at least I'm avoiding a crash on my app when that exception is thrown. What I did was to override the default uncaught exception handler and suppress crashing the app when the uncaught exception is this one. The crash was because of a NullPointerException and it always happened on a GLThread (must be from the drawing code from Google Maps). So I check if all the prerequisites apply, and if so, I send a broadcast with a custom action so I can listen for that broadcast on the fragment on which I use a Google Map, replacing my SupportMapFragment when I get one. Since we got an uncaught exception, the GLThread is interrupted and the drawing to the Google Maps Fragment is suspended, so we do need to replace the Google Maps Fragment for the map to work again.

Here is the code I had to add:

On my Application's onCreate:

final UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        if (ex instanceof NullPointerException && thread.getName().startsWith("GLThread")) {
            for (StackTraceElement stackTraceElement : ex.getStackTrace()) {
                if (stackTraceElement.getClassName().contains("maps")) {
                    sendBroadcast(new Intent(ACTION_MAPS_NPE));
                    return;
                }
            }
        }
        defaultHandler.uncaughtException(thread, ex);
    }
});

And then on my Fragment which uses a Google Maps Fragment, I register this broadcast receiver right before adding the SupportMapFragment to the layout on onCreateView (and unregister it on onDestroyView):

private class GoogleMapsNPEReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Fragment mapFragment = getChildFragmentManager().findFragmentById(R.id.map_fragment_container);
        if (mapFragment != null) {
            getChildFragmentManager().beginTransaction()
                    .replace(R.id.map_fragment_container, SupportMapFragment.newInstance())
                    .commit();
            mMapInitialized = false;
            // We won't have onResume to initialize our map anymore. Try to initialize it after 100ms.
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (getGoogleMap() == null) {
                        handler.postDelayed(this, 100);
                    }
                }
            }, 100);
        }
    }
}

Hope this helps anyone with that same issue. If anyone has an actual fix for it please let me know.

 
精彩推荐