2015-09-08 132 views
1

我有一個應用程序使用導航抽屜。在我的應用程序中,第一個片段(默認)包含谷歌地圖和谷歌地圖API活動。問題是我面臨的是當我用另一個片段替換默認片段它工作正常但當我再次回到我的默認頁面的應用程序崩潰並顯示我的錯誤android.view.InflateException:二進制XML文件行#6:錯誤膨脹類片段

android.view.InflateException:二進制XML文件6號線:錯誤充氣類片段 這裏是我的代碼

public class MainActivity extends FragmentActivity { 
private static String TAG = MainActivity.class.getSimpleName(); 
Boolean flag = false; 
ListView mDrawerList; 
RelativeLayout mDrawerPane; 
private DrawerLayout mDrawerLayout; 
public static FragmentManager fragmentManager; 
ArrayList<NavItem> mNavItems = new ArrayList<NavItem>(); 
static GoogleMap mMap; 
FragmentTransaction fragmentTransaction; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    if (findViewById(R.id.mainContent) != null) { 

     if (savedInstanceState != null) { 
      return; 
     } 
     fragmentManager = getFragmentManager(); 
     fragmentTransaction = fragmentManager.beginTransaction(); 
     HomeFragment homefragment = new HomeFragment(); 
     fragmentTransaction.add(R.id.mainContent, homefragment).commit(); 
     flag = true; 
    } 

    mNavItems.add(new NavItem("Profile",R.drawable.ace)); 
    mNavItems.add(new NavItem("Book A Truck",R.drawable.ace)); 
    mNavItems.add(new NavItem("My Bookings",R.drawable.ace)); 
    mNavItems.add(new NavItem("Farecards",R.drawable.ace)); 
    mNavItems.add(new NavItem("Call Support",R.drawable.ace)); 
    mNavItems.add(new NavItem("About",R.drawable.ace)); 

    // DrawerLayout 
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); 

    // Populate the Navigtion Drawer with options 
    mDrawerPane = (RelativeLayout) findViewById(R.id.drawerPane); 
    mDrawerList = (ListView) findViewById(R.id.navList); 
    DrawerListAdapter adapter = new DrawerListAdapter(this, mNavItems); 
    mDrawerList.setAdapter(adapter); 

    // Drawer Item click listeners 
    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      if (position == 4){ 
       String number = "tel:9643173925"; 
       Intent telephonecall = new Intent(Intent.ACTION_CALL,Uri.parse(number)); 
       startActivity(telephonecall); 
       mDrawerLayout.closeDrawer(mDrawerPane); 
      } else if (position == 1 && flag) { 
       mDrawerLayout.closeDrawer(mDrawerPane); 
      } else { 
       selectItemFromDrawer(position); 
      } 
     } 
    }); 
} 

private void selectItemFromDrawer(int position) { 
    Fragment fragment = new HomeFragment(); 
    Class fragmentClass = null; 
    switch (position) { 
     case 0: 
      fragmentClass = ProfileFragment.class; 
      flag = false; 
      break; 
     case 1: 
      fragmentClass = HomeFragment.class; 
      flag = true; 
      break; 
     case 2: 
      fragmentClass = MybookingFragment.class; 
      flag =false; 
      break; 
     case 3: 
      fragmentClass = FarecardFragment.class; 
      flag = false; 
      break; 
     case 5: 
      fragmentClass = AboutFragment.class; 
      flag = false; 
      break; 
     default: 
      fragmentClass = HomeFragment.class; 
      flag = true; 
      break; 
    } 
    try { 
     fragment = (Fragment) fragmentClass.newInstance(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    String backstackname = fragmentClass.getName(); 
    FragmentManager fragmentManager = getFragmentManager(); 
    fragmentManager.beginTransaction().replace(R.id.mainContent, fragment).addToBackStack(null).commit(); 
    mDrawerLayout.closeDrawer(mDrawerPane); 
} 

我Activity.xml代碼:

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawerLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<!-- The main content view --> 
<FrameLayout 
    android:id="@+id/mainContent" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<!-- The navigation drawer --> 
<RelativeLayout 
    android:layout_width="280dp" 
    android:layout_height="match_parent" 
    android:id="@+id/drawerPane" 
    android:layout_gravity="start"> 

    <!-- Profile Box --> 

    <RelativeLayout 
     android:id="@+id/profileBox" 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:background="#A91C44" 
     android:padding="8dp" > 

     <ImageView 
      android:id="@+id/avatar" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:src="@drawable/ace" 
      android:layout_marginTop="15dp" /> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="42dp" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="15dp" 
      android:layout_toRightOf="@+id/avatar" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/userName" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Sushant" 
       android:textColor="#fff" 
       android:textSize="16sp" 
       android:textStyle="bold" /> 

      <TextView 
       android:id="@+id/desc" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="bottom" 
       android:layout_marginTop="4dp" 
       android:text="View Profile" 
       android:textColor="#fff" 
       android:textSize="12sp" /> 
     </LinearLayout> 
    </RelativeLayout> 

    <!-- List of Actions (pages) --> 
    <ListView 
     android:id="@+id/navList" 
     android:layout_width="280dp" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/profileBox" 
     android:choiceMode="singleChoice" 
     android:background="#ffffffff" /> 

</RelativeLayout> 

錯誤日誌:

android.view.InflateException: Binary XML file line #6: Error inflating class fragment 
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713) 
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
     at in.moovo.route.HomeFragment.onCreateView(HomeFragment.java:105) 
     at android.app.Fragment.performCreateView(Fragment.java:1700) 
     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890) 
     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) 
     at android.app.BackStackRecord.run(BackStackRecord.java:684) 
     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447) 
     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443) 
     at android.os.Handler.handleCallback(Handler.java:733) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:136) 
     at android.app.ActivityThread.main(ActivityThread.java:5086) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
     at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f0e0074, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment 
     at android.app.Activity.onCreateView(Activity.java:4808) 
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689) 
+0

means問題是'activity_main'佈局。分享xml'activity_main'文件xml –

+0

你能否也請向我們展示你的xml代碼? –

回答

1

你在你的activity_main.xml中結束需要</android.support.v4.widget.DrawerLayout>

+0

這可能不是你問題的根源,但你也應該爲你的導航抽屜使用一個片段。 –

相關問題