【问题标题】:getFragmentManager returning null pointer exceptiongetFragmentManager 返回空指针异常
【发布时间】:2014-05-01 20:02:16
【问题描述】:

我在 setUpMapIfNeeded() 函数中的 getFragmentManager 返回null pointer exception。我将我的片段与 activity_main.xml 分开放置,这里是我的代码:

activity_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yai.testmap.MainActivity"
tools:ignore="MergeRootFrame" /> 

更新

fragment_main.xml:

<?xml version="1.0" encoding="utf-8" ?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

<!--  MapView-->

<com.google.android.gms.maps.MapView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
 />

这里是我的 *.java 文件:

更新

包 com.yai.testmap; 导入android.support.v7.app.ActionBarActivity; 导入android.support.v4.app.Fragment; 导入android.os.Bundle; 导入 android.view.LayoutInflater; 导入 android.view.Menu; 导入android.view.MenuItem; 导入android.view.View; 导入android.view.ViewGroup; /* 报告错误 */ 导入android.util.Log; /* 导入使用此项目的下拉列表 */ 导入 android.widget.CheckBox; 导入 android.widget.Spinner; 导入 android.widget.Toast; 导入 android.widget.AdapterView; 导入 android.widget.ArrayAdapter; 导入 android.widget.AdapterView.OnItemSelectedListener; 导入 com.google.android.gms.common.ConnectionResult; 导入 com.google.android.gms.common.GooglePlayServicesNotAvailableException; 导入 com.google.android.gms.common.GooglePlayServicesUtil; /* 导入谷歌地图 */ 导入 com.google.android.gms.maps.GoogleMap; 导入 com.google.android.gms.maps.MapFragment; 导入 com.google.android.gms.maps.MapsInitializer; 导入 com.google.android.gms.maps.SupportMapFragment; 导入 com.google.android.gms.maps.MapView; /* 谷歌地图类型 */ 导入静态 com.google.android.gms.maps.GoogleMap.MAP_TYPE_HYBRID; 导入静态 com.google.android.gms.maps.GoogleMap.MAP_TYPE_NONE; 导入静态 com.google.android.gms.maps.GoogleMap.MAP_TYPE_NORMAL; 导入静态 com.google.android.gms.maps.GoogleMap.MAP_TYPE_SATELLITE; 导入静态 com.google.android.gms.maps.GoogleMap.MAP_TYPE_TERRAIN; 公共类 MainActivity 扩展 ActionBarActivity 实现 OnItemSelectedListener { 私有静态 MapView mMapView; 私有静态 MapFragment mMapFragment; 私有静态 GoogleMap mGoogleMap; 私有静态复选框 mTrafficCheckbox、mLocationCheckbox、mBuildingCheckbox、mIndoorCheckbox; 公共静态类 PlaceholderFragment 扩展片段 { 公共占位符片段(){ } @覆盖 public View onCreateView(LayoutInflater inflater, ViewGroup 容器, 捆绑保存的InstanceState){ 查看 rootView = inflater.inflate(R.layout.fragment_main, 容器, 错误的); Spinner spinner = (Spinner) rootView.findViewById(R.id.spinnerOption); ArrayAdapter 适配器 = ArrayAdapter.createFromResource(getActivity(), R.array.spinner_option_string, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(适配器); spinner.setOnItemSelectedListener((OnItemSelectedListener) getActivity()); mTrafficCheckbox = (CheckBox) rootView.findViewById(R.id.checkbox_traffic); mLocationCheckbox = (CheckBox) rootView.findViewById(R.id.checkbox_location); mBuildingCheckbox = (CheckBox) rootView.findViewById(R.id.checkbox_building); mIndoorCheckbox = (CheckBox) rootView.findViewById(R.id.checkbox_indoor); MapsInitializer.initialize(getActivity()); 开关 (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()) ) { 案例 ConnectionResult.SUCCESS : mMapView = (MapView) rootView.findViewById(R.id.map); mmapView.onCreate(savedInstanceState); 如果(mMapView!= null){ mGoogleMap = mMapView.getMap(); mGoogleMap.getUiSettings().setMyLocationButtonEnabled(false); mGoogleMap.setMyLocationEnabled(true); } } 返回根视图; } } @覆盖 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); } setUpMapIfNeeded(); } 私人无效 setUpMapIfNeeded(){ 如果(mGoogleMap == null){ //mMapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); mGoogleMap = mMapView.getMap(); } } 私人布尔检查就绪(){ 如果(mGoogleMap == null){ Toast.makeText(this, R.string.map_not_ready, Toast.LENGTH_SHORT).show(); 返回假; } 返回真; } 私人无效更新流量(){ 如果(!checkReady()){ 返回; } mGoogleMap.setTrafficEnabled(mTrafficCheckbox.isChecked()); } 公共无效 onTrafficToggled(查看视图){ 更新交通(); } 私人无效更新位置(){ 如果(!checkReady()){ 返回; } mGoogleMap.setMyLocationEnabled(mLocationCheckbox.isChecked()); } public void onLocationToggled(查看视图){ 更新位置(); } 私人无效更新建筑(){ 如果(!checkReady()){ 返回; } mGoogleMap.setBuildingsEnabled(mBuildingCheckbox.isChecked()); } 公共无效 onBuildingToggled(查看视图){ 更新建筑(); } 私人无效更新室内(){ 如果(!checkReady()){ 返回; } mGoogleMap.setIndoorEnabled(mIndoorCheckbox.isChecked()); } 公共无效 onIndoorToggled(查看视图){ 更新室内(); } @覆盖 public void onItemSelected(AdapterView parent, View view, int position, long id){ setLayer((String) parent.getItemAtPosition(position)); } 私人无效setLayer(字符串层名称){ 如果(!checkReady()){ 返回; } if(layerName.equals(getString(R.string.normal))){ mGoogleMap.setMapType(MAP_TYPE_NORMAL); } 否则 if(layerName.equals(getString(R.string.hybrid))){ mGoogleMap.setMapType(MAP_TYPE_HYBRID); } 否则如果(layerName.equals(getString(R.string.satellite))){ mGoogleMap.setMapType(MAP_TYPE_SATELLITE); } 否则如果(layerName.equals(getString(R.string.terrain))){ mGoogleMap.setMapType(MAP_TYPE_TERRAIN); } 否则 if(layerName.equals(getString(R.string.none_map))){ mGoogleMap.setMapType(MAP_TYPE_NONE); } 别的{ Log.i("LDA", "错误设置层名称" + layerName); } } @覆盖 公共无效 onNothingSelected(AdapterView 父){ } @覆盖 受保护的无效 onResume() { 超级.onResume(); setUpMapIfNeeded(); 如果(mGoogleMap!= null){ 更新交通(); 更新位置(); 更新建筑(); 更新室内(); } } @覆盖 公共布尔 onCreateOptionsMenu(菜单菜单){ // 膨胀菜单;如果存在,这会将项目添加到操作栏。 getMenuInflater().inflate(R.menu.main, menu); 返回真; } @覆盖 public boolean onOptionsItemSelected(MenuItem item) { // 处理操作栏项目点击这里。操作栏将 // 自动处理对 Home/Up 按钮的点击,这么久 // 当您在 AndroidManifest.xml 中指定父活动时。 int id = item.getItemId(); 如果(id == R.id.action_settings){ 返回真; } 返回 super.onOptionsItemSelected(item); } }

我尝试将 getSupportFramentManager 与 android.support.v4.app.DialogFragment 支持库一起使用:

mGoogleMap = ((MapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

但仍然无法正常工作。我从 eclipse Cannot cast from Fragment to MapFragment 得到这个错误。 我试图扩展到 FragmentActivity 并仍然返回空指针。但是当我尝试在函数 setUpMapIfNeeded() 中注释 getSupportFragmentManager 时,错误消失了。

这是我的 LogCat:

更新

03-24 20:43:36.124: E/AndroidRuntime(27152): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.yai.testmap/com.yai.testmap.MainActivity}: java.lang.NullPointerException 03-24 20:43:36.124: E/AndroidRuntime(27152): 在 com.yai.testmap.MainActivity.setUpMapIfNeeded(MainActivity.java:104) 03-24 20:43:36.124: E/AndroidRuntime(27152): 在 com.yai.testmap.MainActivity.onCreate(MainActivity.java:98)

有什么想法吗?

【问题讨论】:

  • 扩展 ActionBarFragmentActivity 而不是 ActionBarActivity 并更改此 mGoogleMap = ((SupportMapFragment)getSupportFragmentManager.findFragmentById(R.id.map)).getMap();

标签: android google-maps google-maps-android-api-2


【解决方案1】:

铸造问题可以解决

mGoogleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

如果你想在一个片段中映射你需要使用MapView或者你需要扩展SupportMapFragment

你可以参考下面

Android - android.view.InflateException: Binary XML file line #8: Error inflating class fragment

【讨论】:

  • 您好,感谢您的回复。我试图更改为mGoogleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();,但仍然返回空指针异常。有什么想法吗?
  • @CodingHorror 请检查我发布的链接。
  • @CodingHorror 不能解决 NPE。但是您应该按照链接中的建议使用 MapView。
  • 好的,让我试试。我会在几分钟后回复你。
  • 我刚刚更新了我的问题,我得到了三个错误。 (比以前更好,非常感谢您的链接)。请检查一下。函数setUpMapIfNeeded()又发生了错误,所以我应该在函数setUpMapIfNeeded()中做什么,我真的需要这个函数,因为它被别人调用了很多次
【解决方案2】:

问题是因为在你的xml文件中你使用了

android:name="com.google.android.gms.maps.SupportMapFragment"

在你使用过的java类文件中

mGoogleMap = ((MapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

所以你得到了错误。所以答案是从

mGoogleMap = ((MapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

mGoogleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

【讨论】:

  • 嗨,感谢您的回答,但我已尝试遵循您的建议,但仍然无效。有什么想法吗?
  • 是的,仍然是 NPE。我很确定这部分代码返回 null。有什么想法吗?
  • @CodingHorror 只需查看 raghunandan 发布的链接。它会对你有所帮助。
【解决方案3】:

改变这个

mGoogleMap = ((MapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

mGoogleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

【讨论】:

    猜你喜欢
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多