这是我的 MainActivity.java 代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fr_mgr = getFragmentManager();
MapFragment map_frag = (MapFragment) fr_mgr.findFragmentById(R.id.map);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
map = map_frag.getMap();
map.setMyLocationEnabled(true);
LatLng Bhopal = new LatLng(23.233243200000000000, 77.434339400000000000);
map.addMarker(new MarkerOptions()
.position(new LatLng(23.233243200000000000, 77.434339400000000000))
.title("Hello Bhopal")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
Polygon polygon = map.addPolygon(new PolygonOptions()
.add(new LatLng(23.226834, 77.355309),
new LatLng(23.214845, 77.42672),
new LatLng(23.187707, 77.388954),
new LatLng(23.200961, 77.31411)).strokeColor(Color.RED)
.fillColor(Color.parseColor("#51000000")).strokeWidth(2));
}
}
这是我的布局文件“activity_main.xml”:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
最佳答案
已解决的问题:
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
代码:
import android.graphics.Color;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;
public class MapAPIV2DemoActivity extends FragmentActivity
{
GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_apiv2_demo);
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.setMyLocationEnabled(true);
LatLng Bhopal = new LatLng(23.233243200000000000, 77.434339400000000000);
mMap.addMarker(new MarkerOptions()
.position(new LatLng(23.233243200000000000, 77.434339400000000000))
.title("Hello Bhopal")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
Polygon polygon = mMap.addPolygon(new PolygonOptions()
.add(new LatLng(23.226834, 77.355309),
new LatLng(23.214845, 77.42672),
new LatLng(23.187707, 77.388954),
new LatLng(23.200961, 77.31411)).strokeColor(Color.RED)
.fillColor(Color.parseColor("#51000000")).strokeWidth(2));
}
}
关于android - MapView 中的致命和 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18457950/