我正在尝试使用ViewPager的Android应用程序。在第三页中,我有一张地图,我在片段的xml中实例化:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity"
android:orientation="vertical">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
class="com.google.android.gms.maps.SupportMapFragment" />
...
</RelativeLayout>
因此,当我尝试在通过地图ID扩展片段的类的 onStart()之后获取地图时,我得到 NullPointerException 。
public void onStart() {
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
mMap = mapFragment.getMap();
esperandoLoc.setVisibility(View.VISIBLE);
inicialitzarLoc();
fragmentMap = this;
if (mMap== null)
{
Toast.makeText(getActivity(),"Google Maps not Available",
Toast.LENGTH_LONG).show();
}
}
Debuggin我发现FragmentManager正确地包含了ViewPager的五个片段,但是这个片段里面的片段不存在。
知道如何正确获取地图?谢谢。
修改 我忘了说我在片段类中的 onCreate()中膨胀了包含地图的片段:
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_mapa, container, false);
运行后获得的logcat错误是下一个错误:
05-18 20:41:29.725: E/AndroidRuntime(20636): FATAL EXCEPTION: main
05-18 20:41:29.725: E/AndroidRuntime(20636): Process: com.example.app, PID: 20636
05-18 20:41:29.725: E/AndroidRuntime(20636): java.lang.NullPointerException
05-18 20:41:29.725: E/AndroidRuntime(20636): at fragments.MapaTab.inicialitzarMapa(MapaTab.java:113)
05-18 20:41:29.725: E/AndroidRuntime(20636): at fragments.MapaTab.inicialitzarLoc(MapaTab.java:108)
05-18 20:41:29.725: E/AndroidRuntime(20636): at fragments.MapaTab.onActivityCreated(MapaTab.java:90)
05-18 20:41:29.725: E/AndroidRuntime(20636): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:892)
05-18 20:41:29.725: E/AndroidRuntime(20636): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
05-18 20:41:29.725: E/AndroidRuntime(20636): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
05-18 20:41:29.725: E/AndroidRuntime(20636): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
05-18 20:41:29.725: E/AndroidRuntime(20636): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
05-18 20:41:29.725: E/AndroidRuntime(20636): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
05-18 20:41:29.725: E/AndroidRuntime(20636): at android.support.v4.view.ViewPager.populate(ViewPager.java:895)
05-18 20:41:29.725: E/AndroidRuntime(20636): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:469)
05-18 20:41:29.725: E/AndroidRuntime(20636): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:441)
05-18 20:41:29.725: E/AndroidRuntime(20636): at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:422)
05-18 20:41:29.725: E/AndroidRuntime(20636): at com.example.app.MainActivity.onTabSelected(MainActivity.java:136)
05-18 20:41:29.725: E/AndroidRuntime(20636): at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:577)
05-18 20:41:29.725: E/AndroidRuntime(20636): at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1105)
05-18 20:41:29.725: E/AndroidRuntime(20636): at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:547)
05-18 20:41:29.725: E/AndroidRuntime(20636): at android.view.View.performClick(View.java:4438)
05-18 20:41:29.725: E/AndroidRuntime(20636): at android.view.View$PerformClick.run(View.java:18422)
05-18 20:41:29.725: E/AndroidRuntime(20636): at android.os.Handler.handleCallback(Handler.java:733)
05-18 20:41:29.725: E/AndroidRuntime(20636): at android.os.Handler.dispatchMessage(Handler.java:95)
05-18 20:41:29.725: E/AndroidRuntime(20636): at android.os.Looper.loop(Looper.java:136)
05-18 20:41:29.725: E/AndroidRuntime(20636): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-18 20:41:29.725: E/AndroidRuntime(20636): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 20:41:29.725: E/AndroidRuntime(20636): at java.lang.reflect.Method.invoke(Method.java:515)
05-18 20:41:29.725: E/AndroidRuntime(20636): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-18 20:41:29.725: E/AndroidRuntime(20636): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-18 20:41:29.725: E/AndroidRuntime(20636): at dalvik.system.NativeStart.main(Native Method)