2014-10-28 109 views
1

我想在Android中創建應用,並且我想在導航抽屜中添加地圖。抽屜選項打開新的碎片,但無法使Map碎片工作。無法將谷歌地圖添加到導航抽屜片段

MainActivity.java設置片段方式如下:

/** 
* Diplaying fragment view for selected nav drawer list item 
* */ 
private void displayView(int position) { 
    // update the main content by replacing fragments 
    Fragment fragment = null; 
    switch (position) { 
    case 0: 
     fragment = new HomeFragment(); 
     break; 
    case 1: 
     fragment = new PoliFragment(); 
     break; 
    case 2: 
     fragment = new TilefonaFragment(); 
     break; 
    case 3: 
     fragment = new DromologiaFragment(); 
     break; 
    case 4: 
     fragment = new XartisFragment(); 
     break; 

    default: 
     break; 
    } 

    if (fragment != null) { 
     FragmentManager fragmentManager = getFragmentManager(); 
     fragmentManager.beginTransaction() 
       .replace(R.id.frame_container, fragment).commit(); 

     // update selected item and title, then close the drawer 
     mDrawerList.setItemChecked(position, true); 
     mDrawerList.setSelection(position); 
     //setTitle(navMenuTitles[position]); 
     mDrawerLayout.closeDrawer(mDrawerList); 
    } else { 
     // error in creating fragment 
     Log.e("MainActivity", "Error in creating fragment"); 
    } 
} 

的地圖片段(XartisFragment.java)如下:

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
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.SupportMapFragment; 


public class XartisFragment extends Fragment { 
private GoogleMap map; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.map_layout,container, false); 

    map = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();//((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap(); 
    return rootView; 
} 


} 

map_layout.xml是以下內容:

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

<fragment 
    android:id="@+id/map" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginTop="8dp" /> 


</RelativeLayout> 

當我嘗試運行它,我得到:

Error:(178, 15) error: incompatible types: XartisFragment cannot be converted to Fragment 

它指向錯誤此行MainActivity.java關於以使其加載哪些改變

fragment = new XartisFragment(); 

任何想法?

+0

import android.support.v4.app.Fragment;在哪裏displayView()方法類 – 2014-10-28 10:58:45

+1

fragmentManager.beginTransaction() .replace(R.id.frame_container,fragment).commit();錯誤從此處拋出。 – 2014-10-28 10:59:29

回答

1

嘗試使用getSupportFragmentManager()而不是getFragmentManager()

確保兩個地方都導入相同的Fragment類。感覺有點像在一個地方你正在導入android.app.Fragment(原生API級別11版本的碎片),並在其他地方導入android.support.v4.app.Fragment(來自Android支持的碎片包)。

+0

非常感謝@Duggu。這似乎是問題,改變一切android.app.Fragment(因爲我不想支持舊設備),並刪除了所有提到支持類,現在它正確加載。 – duk3r 2014-10-28 11:45:45