0
所以我想添加對象到地圖我有這樣的圓形,當用戶點擊「添加圓」按鈕,但是當我嘗試從xml中獲取地圖時,我得到一個nullpointerexeption當它的.getmap()
下newCircle()
功能的任何修補程序或解決方法?示例代碼很有幫助。在Android上添加Google地圖對象
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"
/>
就是這樣!謝謝。 – 2013-03-14 19:48:51