【发布时间】:2014-05-20 16:08:36
【问题描述】:
我的代码在我的代码的这一行抛出了NullPointerException:
map=((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap()
我可以看到它在 LogCat 中引发了异常。我已经尝试过多次解决这个问题,并且我已经阅读了这个论坛上所有类似的问题。我尝试了一些解决方案,但徒劳无功。当我注释掉该行时,我会显示 Google 地图,但是当我尝试在我的代码中获取片段时,我仍然会遇到同样的异常。
这是我的代码:
package cs.exmpl;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
public class MainActivity extends ActionBarActivity {
private GoogleMap map;
private final LatLng loc=new LatLng(566544, 556554);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map=((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
//SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// map = mapFrag.getMap();
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
CameraUpdate up=CameraUpdateFactory.newLatLngZoom(loc, 14);
map.animateCamera(up);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
这是我的 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cs.exmpl.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_below="@+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
【问题讨论】:
-
将
map=((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();移动到 onStart。视图可能尚未完全绘制,因此它可能返回 null。 -
你能说得更清楚些吗?我不明白你的意思谢谢你
-
setContentView 表示您想要扩充包含地图视图的布局,但它不会立即执行此操作。一旦您的视图完成绘制(因此您的片段已准备好被获取),就会调用 onStart。
-
@user3448582 什么是 min sdk 是否低于 11,因为您扩展了
ActionBarActivity在这种情况下您将需要使用SupportMapFragment -
你能给出能解决我问题的指令吗
标签: android google-maps maps