3

我使用的動作條。不同的標籤具有不同的佈局,並且在這些標籤中的一個上,我嵌入了一個佈局,其中包含一個MapFragment。使用下面的代碼,該應用程序運行良好,mapFragments工作正常,選項卡正常工作。但是,一旦我想訪問嵌入在myFragment中的mapFragment,使用getFragmentManager()。findFragmentById(R.id.map_add),我總是得到null。findFragmentById總是返回在MainActivity在導航模式下空

我試過使用FragmentManager和FragmentTransaction來添加myfragment和MapFragment,但仍然從findFragmentById(R.id.map_add)中獲得null。請幫助...

在MainActivity初始化是這樣的:

protected void onCreate(Bundle bundle) { 
    super.onCreate(bundle); 
    setContentView(R.layout.main); 

    // ActionBar gets initiated 
    ActionBar actionbar = getActionBar(); 
    // Tell the ActionBar we want to use Tabs. 
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    // initiating the tabs and set text to it. 
    ActionBar.Tab myTab = actionbar.newTab().setText("hey"); 

    // create the fragments we want to use for display content 
    Fragment myFragment = new myFragment(); 

    // set the Tab listener. Now we can listen for clicks. 
    myTab.setTabListener(new MyTabsListener(myFragment)); 


    // add the tabs to the actionbar 
    actionbar.addTab(myTab); 
    map=((MapFragment)getFragmentManager().findFragmentById(R.id.map_add)).getMap();//line 103 

} 

在MainActivity的佈局,main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/fragment_container" 
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=".MainActivity" > 
</RelativeLayout> 

的tablistener的代碼:

class MyTabsListener implements ActionBar.TabListener { 
    public Fragment fragment; 

    public MyTabsListener(Fragment fragment) { 
     this.fragment = fragment; 
    } 

    @Override 
    public void onTabReselected(Tab tab, FragmentTransaction ft) { 

    } 

    @Override 
    public void onTabSelected(Tab tab, FragmentTransaction ft) { 
     ft.replace(R.id.fragment_container, fragment);   
    } 

    @Override 
    public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
     ft.remove(fragment); 
    } 

} 

myFragment代碼:

public static class AddAliasFragment extends Fragment { 

    private static View view; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     if (view != null) { 
      ViewGroup parent = (ViewGroup) view.getParent(); 
      if (parent != null) 
       parent.removeView(view); 
     } 
     try { 
      view = inflater.inflate(R.layout.myfragment_layout, container, false); 
     } catch (InflateException e) { 
      /* map is already there, just return view as it is */ 
     } 
     return view; 
    } 

    public MapFragment getMapFragment(){ 
     return (MapFragment)getFragmentManager().findFragmentById(R.id.map_add); 
    } 

} 

myFragment的佈局:myfragment_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/map_add_layout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<EditText 
    android:id="@+id/search" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:hint="@string/searchhint" 
    /> 

<Button 
    android:id="@+id/searchbtn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/search" 
    android:layout_toRightOf="@id/search" 
    android:text="@string/go" /> 

<fragment 
     android:id="@+id/map_add" 
     android:tag="add" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/search" 
     android:name="com.google.android.gms.maps.MapFragment"/> 

的logcat的輸出:所述空指針的異常是指線103,這是的GetMap()線,我在後它評論前面提到的代碼

04-02 21:23:12.122: E/AndroidRuntime(3672): FATAL EXCEPTION: main 
04-02 21:23:12.122: E/AndroidRuntime(3672): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.test.test/com.test.test.MainActivity}:  java.lang.NullPointerException 
04-02 21:23:12.122: E/AndroidRuntime(3672):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307) 
04-02 21:23:12.122: E/AndroidRuntime(3672):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357) 
04-02 21:23:12.122: E/AndroidRuntime(3672):  at android.app.ActivityThread.access$600(ActivityThread.java:153) 
04-02 21:23:12.122: E/AndroidRuntime(3672):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247) 
04-02 21:23:12.122: E/AndroidRuntime(3672):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-02 21:23:12.122: E/AndroidRuntime(3672):  at android.os.Looper.loop(Looper.java:137) 
04-02 21:23:12.122: E/AndroidRuntime(3672):  at android.app.ActivityThread.main(ActivityThread.java:5226) 
04-02 21:23:12.122: E/AndroidRuntime(3672):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-02 21:23:12.122: E/AndroidRuntime(3672):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-02 21:23:12.122: E/AndroidRuntime(3672):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) 
04-02 21:23:12.122: E/AndroidRuntime(3672):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 
04-02 21:23:12.122: E/AndroidRuntime(3672):  at dalvik.system.NativeStart.main(Native Method) 
04-02 21:23:12.122: E/AndroidRuntime(3672): Caused by: java.lang.NullPointerException 
04-02 21:23:12.122: E/AndroidRuntime(3672):  at  com.test.test.MainActivity.onCreate(MainActivity.java:103) 
04-02 21:23:12.122: E/AndroidRuntime(3672):  at android.app.Activity.performCreate(Activity.java:5104) 
04-02 21:23:12.122: E/AndroidRuntime(3672):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
04-02 21:23:12.122: E/AndroidRuntime(3672):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261) 
04-02 21:23:12.122: E/AndroidRuntime(3672):  ... 11 more 
+0

也顯示logcat錯誤。 –

+0

您正在使用相同的片段map_add。 – Unknown

+0

@LalitPoptani我更新了logcat輸出 – WTH

回答

3

發生這種情況是因爲MapFragment還沒有完成加載谷歌地圖對象。要解決此問題,請在創建地圖片段時嘗試覆蓋onActivityCreated。從onActivityCreated的實現中,執行所有的地圖訪問和修改。

myMapFragment=new SupportMapFragment(){ 
    @Override 
    public void onActivityCreated(Bundle savedInstanceState){ 
     //modify your map here 
    } 
} 
+0

從我所知道的,onActivityCreated是通知片段該活動已完成onCreate()。不過,我試圖在activity的onCreate()中使用getMap()。因此,即使我重寫片段的onActivityCreated(),我仍然會在它之前訪問getMap(),是嗎? – WTH

+0

經過簡單的測試,我明白你的意思了。我應該嘗試在onActivityCreated()函數中使用getMap(),它確保地圖對象已完成加載。這很好地解決了我的問題。非常感謝您的幫助。 – WTH

+0

是的,這就是我的意思它不會在onCreate方法中可用,所以你需要確保它已經被創建之前,你可以使用onActivityCreated訪問它。不需要感謝,只需點擊接受我的答案。 – chefburns

1

在mi情況下,問題是沒有指定SupportMapFragment。 我有這樣使用:

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/map" 
    android:name="com.google.android.gms.maps.MapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    class="com.google.android.gms.maps.SupportMapFragment" /> 

如這裏https://stackoverflow.com/a/16663713/2411379解釋了谷歌地圖V2使用。