SupportMapFragment或GoogleMap的为空 [英] SupportMapFragment or GoogleMap is null

查看:171
本文介绍了SupportMapFragment或GoogleMap的为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设法获得codeS无错,但是在启动,我总是莫名其妙地在线路MMAP = mapFrag.getMap()得到一个空指针异常

为什么会这样呢?我缺少一些进口或某些步骤?我不确定如果它是SupportMapFragment或GoogleMap的对象,这是造成这个问题。

 包com.fragments;

进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;

进口com.google.android.gms.maps.GoogleMap;
进口com.google.android.gms.maps.SupportMapFragment;

公共类MapFragment扩展SherlockMapFragment {

    私人GoogleMap的MMAP;

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
        查看根= super.onCreateView(充气,容器,savedInstanceState);
        SupportMapFragment mapFrag =(SupportMapFragment)getSupportFragmentManager()findFragmentById(R.id.fragment_map)。
        MMAP = mapFrag.getMap(); //空指针是在这里
        返回根;
    }
}
 

修改:这是基于对这个问题的<一个给定的解决方案实施的一部分href="http://stackoverflow.com/questions/13721929/using-actionbarsherlock-with-the-new-supportmapfragment">here

解决方案

请注意,你并不需要使用自定义的片段子一定要用地图V2。如果你的片段只是单纯的地图,你可以从该活动创建 MapFragment SupportMapFragment 和配置它。

你甚至都不需要建立某种形式的 SherlockMapFragment ,才能够有一个地图是一个ActionBarSherlock为基础的应用程序的一部分。常规 SupportMapFragment 工作得很好。

如果你想有更多的商业智慧在你的片段的如果您使用的是ActionBarSherlock,的有问题的业务逻辑,需要做相关的ActionBarSherlock事情的(例如,促进行动项目的操作栏)的然后的也只有那么你需要担心有某种 SherlockMapFragment

我可以证实,这个主旨包含一个工作 SherlockMapFragment 。请注意,进入 com.actionbarsherlock.app <​​/ code>包,因为它需要ActionBarSherlock其余部分包保护访问。

您可以再子类化,比如创建一个 MyMapFragment

 公共类MyMapFragment扩展SherlockMapFragment {
  @覆盖
  公共无效onActivityCreated(包savedInstanceState){
    super.onActivityCreated(savedInstanceState);

    如果(的GetMap()!= NULL){
      Log.d(的getClass()getSimpleName(),地图准备使用!);
    }
  }
}
 

您必须小心一点上调用的GetMap()的时机 - 太快,它会返回 onActivityCreated()似乎是一个比较安全的时间,虽然你可以自由进行试验。

然后,你只需要使用 MyMapFragment 只要你会使用 SupportMapFragment

 &LT;片段的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID /图
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    类=com.commonsware.android.mapsv2.sherlock.MyMapFragment/&GT;
 

下面是含有上述$ C完整的项目 $ C。

Managed to get the codes error free, however upon launching, I somehow always get a null pointer exception at the line mMap = mapFrag.getMap();

Why is this so? Am I missing some imports or some steps? I am unsure if it's the SupportMapFragment or GoogleMap object that's causing the problem.

package com.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class MapFragment extends SherlockMapFragment {

    private GoogleMap mMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = super.onCreateView(inflater, container, savedInstanceState);
        SupportMapFragment mapFrag= (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_map);
        mMap = mapFrag.getMap(); //null pointer is here
        return root;
    }
}

Edit: This is part of the implementation based on the solution given in this question here

解决方案

Note that you do not need to use a custom Fragment subclass necessarily to use Maps V2. If your fragment is just purely a map, you can create the MapFragment or SupportMapFragment from the activity and configure it there.

You do not even need to create some sort of SherlockMapFragment to be able to have a map be part of an ActionBarSherlock-based app. The regular SupportMapFragment works just fine.

If you do want to have more business smarts in your fragment, and if you are using ActionBarSherlock, and the business logic in question needs to do things related to ActionBarSherlock (e.g., contribute action items to the action bar), then and only then do you need to worry about having some sort of SherlockMapFragment.

I can confirm that this gist contains a working SherlockMapFragment. Note that it goes into the com.actionbarsherlock.app package, as it needs some package-protected access to the rest of ActionBarSherlock.

You can then subclass that, such as creating a MyMapFragment:

public class MyMapFragment extends SherlockMapFragment {
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (getMap() != null) {
      Log.d(getClass().getSimpleName(), "Map ready for use!");
    }
  }
}

You have to be a bit careful on the timing of calling getMap() -- too soon and it will return null. onActivityCreated() seems like a fairly safe time, though you are free to experiment.

Then, you just use MyMapFragment wherever you would have used SupportMapFragment:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.commonsware.android.mapsv2.sherlock.MyMapFragment"/>

Here is the complete project containing the above code.

这篇关于SupportMapFragment或GoogleMap的为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆