我想要点击标记,以便我执行下面链接所说的内容。 clickable marker 1
在扩展android.support.v4.app.FragmentActivity后,我在
上得到空指针异常mapView = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
我的所有代码都太长了所以我给出了一些部分:
public class GpsMapActivity extends android.support.v4.app.FragmentActivity implements OnMarkerClickListener, LocationListener {
private GoogleMap mapView;
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.myAccount:
Intent intent = new Intent(GpsMapActivity.this, MyAccountActivity.class);
startActivity(intent);
return true;
case R.id.announcements:
Intent intent2 = new Intent(GpsMapActivity.this, AnnouncementsActivity.class);
startActivity(intent2);
return true;
}
return false;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gps_map_activity);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
mapView = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mapView.setOnMarkerClickListener((OnMarkerClickListener)this);
gps_map_activity.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="300dp"
android:orientation="vertical"
android:id="@+id/layone"
>
<fragment
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:text="Insert Event"
android:id="@+id/insertevent" />
<Button
android:text="Back to View"
android:id="@+id/backtoview" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:text="to Up"
android:id="@+id/toUp" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:text="to Left"
android:id="@+id/toLeft" />
<Button
android:text="to Right"
android:id="@+id/toRight" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:text="to Down"
android:id="@+id/toDown" />
</TableRow>
</TableLayout>
</LinearLayout>
为什么我得到空指针异常?任何想法?
答案 0 :(得分:1)
你有
class="com.google.android.gms.maps.SupportMapFragment"
但是当你初始化时,你有
mapView = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
应该是
mapView = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
如果GoogleMap
不可用,getMap()
将返回null。因此,在初始化地图对象之前,请更好地检查Google Play服务的可用性