我正在实现一个包含在片段中的谷歌地图视图。当我尝试获取 的 ID 时mapsView
,我得到了一个nullpointer
异常getSupportFragmentManager()
,即使我的类扩展了FragmentActivity
。这是我的代码:
主要活动:
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
public class MainActivity extends Activity implements OnTabChangeListener {
private TabHost mTabHost;
private Layouts l;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Util.setMainActivty(this);
l = new Layouts();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
public void onClick(View v) {
l.setMapsLayout();
}
}
我的 maps_layout 片段
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
布局类:
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.text.GetChars;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.SupportMapFragment;
public class Layouts extends FragmentActivity {
private MapView mMapView;
public void setMapsLayout() {
MainActivity mainActivity = Util.getMainActivity();
ViewGroup layoutContainer = (ViewGroup) mainActivity
.findViewById(R.id.layoutContainer);
layoutContainer.removeAllViews();
View child1 = LayoutInflater.from(mainActivity).inflate(
R.layout.maps_layout, null);
layoutContainer.addView(child1);
GoogleMap mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
}
}
当我得到getSupportFragmentManager()
它的值是空的。这就是我所看到的:
有人知道问题是什么吗?谢谢!