0

У меня есть приложение, которое имеет карту во втором меню в drawerlayout. Когда я пытаюсь раздуть фрагмент с картой, приложение падает.Android: Ошибка раздувания фрагмента с картой в drawerlayout

Ниже приводится код:

if (rootView != null) { 
     ViewGroup parent = (ViewGroup) rootView.getParent(); 
     if (parent != null) { 
      parent.removeView(rootView); 
     } 
    } 

    try { 
     rootView = inflater.inflate(R.layout.fragment_map, container, false); 


    } catch (Exception e) { 

     e.printStackTrace(); 
    } 

Ниже исключение генерируется:

android.view.InflateException: Binary XML файл строка # 23: фрагмент Ошибка при наполнении класса

Я попробовал много решений на стеке и других платформах, но напрасно.

Это, как я использую фрагмент карты в пределах родительского RelativeLayout в моем XML:

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

Я также попытался удалить фрагмент карты в его onDestroyView Митос:

@Override 
public void onDestroyView() { 

    super.onDestroyView(); 

    SupportMapFragment fragment = ((SupportMapFragment) getChildFragmentManager() 
      .findFragmentById(R.id.map)); 
    try{ 
     if (fragment != null) 
      getChildFragmentManager().beginTransaction().remove(fragment).commit(); 
    }catch(Exception e){ 
    } 


} 

Ниже файл манифеста данные:

<meta-data android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 
    <uses-library android:name="com.google.android.maps" /> 
    <activity 
     android:name="com.app.myapp.SplashActivity" 
     android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" 
     android:label="@string/app_name" 
     android:noHistory="true" 
     android:screenOrientation="portrait" 
     android:configChanges="orientation|keyboardHidden|screenSize" 
     android:launchMode="singleTop" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity android:name="com.app.myapp.LoginActivity" 
     android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" 
       android:label="@string/app_name" 
       android:launchMode="singleTop" 
       android:screenOrientation="portrait" 
      android:configChanges="orientation|keyboardHidden|screenSize" 
       > 
    </activity> 

    <activity 
     android:name="com.app.myapp.MainActivity" 
     android:launchMode="singleTop" 
     android:screenOrientation="portrait" 
     android:configChanges="orientation|keyboardHidden|screenSize" 
     android:label="@string/app_name" > 

    </activity> 

+0

В соответствии с моим аспектом вы должны удалить 'карту Fragment' из' Фрагмент Transaction', когда вы идете в другую «Фрагмент» или «Экран». –

+0

Да, я сделал это в методе OnDestroyView, но это не решило мою проблему. Проблема в том, что фрагмент с ошибкой карты при первом запуске. –

+0

Как ??? Опубликуйте свой код ... также опубликуйте свой манифест –

ответ

0

Мы не можем раздуть статических фрагментов в классе, который расширяет фрагмент. Вместо этого вы можете заменить/добавить фрагмент (MapFragment) динамически с помощью кода, как показано ниже.

<?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" 
android:background="@android:color/white" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="top" 
     android:layout_margin="0dp" 
     android:layout_weight="2" 
     android:background="@android:color/transparent" > 

     <FrameLayout 
      android:id="@+id/fragContainer" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/top_layout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <EditText 
      android:id="@+id/inputSearch" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:drawableLeft="@android:drawable/ic_menu_search" 
      android:hint="Search Tag" 
      android:inputType="text" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="bottom" 
     android:layout_margin="0dp" 
     android:layout_weight="2" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" > 

      <ProgressBar 
       android:id="@+id/progressBar" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_vertical|center_horizontal" 
       android:indeterminateDrawable="@drawable/my_progress_indeterminate" 
       android:visibility="gone" > 
      </ProgressBar> 

      <ListView 
       android:id="@+id/crowdmap_list" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:drawSelectorOnTop="false" 
       android:listSelector="@drawable/abc_list_selector_holo_dark" /> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

</RelativeLayout> 

и с помощью кода теперь вы можете добавить/заменить mapframent к этому FrameLayout, как показано ниже

FragmentManager fm = getChildFragmentManager(); 
fm.beginTransaction().replace(R.id.fragContainer, SupportMapFragment.newInstance(), "map").commit(); 
Смежные вопросы