0

因此,当用户点击“添加圆圈”按钮时,我想将对象添加到我拥有的地图中,例如圆圈,但是当我尝试从 xml 获取地图时,当它执行任何修复或解决方法时,我会NullPointerException得到一个? 示例代码很有帮助。.getmap()newCircle()

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;

public class GoogleMaps extends FragmentActivity {
    GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_google_maps);
        makeCircleButton();
    }

    private void makeCircleButton() {
        OnClickListener onClickListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                newCircle();
            }
        };

        Button button = (Button) findViewById(R.id.add_circular_area);
        button.setOnClickListener(onClickListener);
    }

    public void newCircle() {
        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();

        // Instantiates a new CircleOptions object and defines the center and
        // radius
        CircleOptions circleOptions = new CircleOptions().center(
                new LatLng(37.4, -122.1)).radius(1000); // In meters
        mMap.addCircle(circleOptions);

        // Get back the mutable Circle
        // Circle circle = mMap.addCircle(circleOptions);
    }

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <include
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        layout="@layout/fragment" />

    <ScrollView
        android:id="@+id/map_scrollview"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:fadingEdge="none"
        android:scrollbarAlwaysDrawVerticalTrack="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <Button
                android:id="@+id/button1"
                android:layout_width="245dp"
                android:layout_height="wrap_content"
                android:text="Add rectangular area" />

            <Button
                android:id="@+id/add_circular_area"
                android:layout_width="245dp"
                android:layout_height="wrap_content"
                android:text="Add circular area" />

        </LinearLayout>

    </ScrollView>

</RelativeLayout>

xml 包括:

<?xml version="1.0" encoding="utf-8"?>
<fragment
        xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        map:uiTiltGestures="false"
        map:mapType="hybrid"
        />
4

1 回答 1

2

你已经使用了你的片段名称,com.google.android.gms.maps.SupportMapFragment所以你不能得到MapFragment你应该使用getSupportFragmentManager()而不是getFragmentManager()并将其转换为SupportMapFragment

改变

  mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();

mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();

希望它会有所帮助。

于 2013-03-14T04:58:25.013 回答