Android 谷歌地图v2空指针异常
我想点击标记,这样我就做了链接下面说的。 扩展android.support.v4.app.FragmentActivity后,我在Android 谷歌地图v2空指针异常,android,google-maps-android-api-2,Android,Google Maps Android Api 2,我想点击标记,这样我就做了链接下面说的。 扩展android.support.v4.app.FragmentActivity后,我在 mapView = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 我的全部代码太长,因此我给出了一些部分: public class GpsMapActivity extends android.support.v4.app.FragmentActivit
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\u map\u 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>
为什么会出现空指针异常?有什么想法吗?你有吗
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。所以在初始化map object之前最好检查一下google play服务的可用性谢谢,我本来想按你说的更改它,但问题是,当我更改你说的内容时,会出现类似SupportMapFragment这样的错误,无法按类型解决:SimportSupportMapFragment
我现在导入了,但导入时会显示同样,这个SupportMapFragment也无法解决,非常感谢。import语句类似于import com.google.android.gms.maps.SupportMapFragment;你能发布你的logcat错误片段吗?