【问题标题】:Android [SupportMapFragment] The method findViewById(int) is undefined for the typeAndroid [SupportMapFragment] 方法 findViewById(int) 未定义类型
【发布时间】:2012-12-07 16:08:01
【问题描述】:
public class PlaceMapsFragment extends SupportMapFragment {
    private GoogleMap mMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View v = inflater.inflate(R.layout.fragment_place_maps, container,
                false);


        setUpMapIfNeeded();
        return v;
    }

    private void setUpMapIfNeeded() {
        if (mMap == null) {
            mMap = ((MapView) findViewById(R.id.map)).getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title(
                "Marker"));
    }
}

错误

The method findViewById(int) is undefined for the type PlaceMapsFragment

【问题讨论】:

    标签: android android-fragments google-play-services supportmapfragment


    【解决方案1】:

    如果我没记错的话,所有片段都没有原生的findViewById() 方法。这就是你必须使用 onCreateView() 方法的原因。

    这意味着您应该将 setUpMapIfNeeded() 更改为 setUpMapIfNeeded(View view) 并在 onCreateView() 中将 v 传递给您修改后的方法。您还需要将mMap = ((MapView) findViewById(R.id.map)).getMap(); 更改为mMap = ((MapView) view.findViewById(R.id.map)).getMap();

    【讨论】:

      【解决方案2】:

      我不确定您是否可以继承 SupportMapFragment 类。

      要获取SupportMapFragment 的新实例,您可以调用SupportMapFragment.newInstance,这会返回您的对象。

      如果您将其子类化并调用,例如,PlaceMapsFragment.newInstance(),您仍将返回一个 SupportMapFragment 对象。

      如果您想以编程方式操作地图,我建议您执行以下操作。

      this.mSupportMapFragment = SupportMapFragment.newInstance();
      MapHandler mMapHandler = new mMapHandler(this.mSupportMapFragment.getMap());
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2014-01-25
        • 1970-01-01
        • 2015-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多