2013-10-01 149 views
2

我在stackoverflow中發現類似的主題,但它並沒有幫助我。 我顯示谷歌地圖使用片段,它得到另一個片段後回來並崩潰。 換句話說,谷歌地圖只顯示一次,崩潰。 這裏是代碼。android.view.InflateException:二進制XML文件行#20:錯誤膨脹類片段

public class MapTabMainFragment extends BaseFragment { 
    private AdView adView; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View view = null; 
     try { 
      view = inflater.inflate(R.layout.fragment_map_main, container, 
        false); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     initComponents(view); 
     initValues(); 
     initListeners(); 

     return view; 
    } 

    public void initComponents(View view) { 
     adView = (AdView) view.findViewById(R.id.adView1); 
    } 

    public void initValues() { 
     AdRequest re = new AdRequest(); 
     adView.loadAd(re); 
    } 

    public void initListeners() { 

    } 
} 

public class BaseFragment extends Fragment { 

    public BottomTabActivity mActivity; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     mActivity = (BottomTabActivity) this.getActivity(); 
    } 

    public boolean onBackPressed() { 
     return false; 
    } 

    public void onActivityResult(int requestCode, int resultCode, Intent data) { 

    } 
} 

當我試圖捕獲異常,這是

android.view.InflateException:二進制XML文件行#20:錯誤充氣類片段。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/lib/com.google.ads" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/WhiteColor" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/header" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="@dimen/data_listview_margin_top" > 

     <fragment 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/map" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginBottom="@dimen/data_listview_margin_bottom" 
      class="com.google.android.gms.maps.SupportMapFragment" /> 
     <!--class="com.cyrilmottier.polaris2.maps.SupportMapFragment" --> 

     <com.google.ads.AdView 
      android:id="@+id/adView1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      app:adSize="SMART_BANNER" 
      app:adUnitId="ca-app-pub-9766031373541061/3761995838" 
      app:loadAdOnCreate="true" 
      app:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" > 
     </com.google.ads.AdView> 
    </RelativeLayout> 

</LinearLayout> 
+0

該錯誤是在XML文件中。你可以請張貼嗎? – Szymon

+0

@Szymon - 我已發佈XML文件 – user2291109

回答

0
// try this way 

public class MapTabMainFragment extends FragmentActivity { 

    private AdView adView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fragment_map_main); 
     adView = (AdView) findViewById(R.id.adView1); 
     AdRequest re = new AdRequest(); 
     adView.loadAd(re); 

    } 

} 
1

我對計算器的研究後解決它自己。 這與嵌套的片段問題有關。 我在xml中刪除了地圖片段,併爲它放置了框架,然後以編程方式添加了片段。 它像一個魅力。 感謝您的一切努力。

+0

請將此標記爲此問題的選定答案。 – Emmanuel

+0

請分享代碼。 – VenomVendor

+0

@ user2291109請分享代碼。我也需要它 –

0

您正在嘗試之前它在你的BaseFragment類實際創建訪問活動......

mActivity = (BottomTabActivity) this.getActivity(); 

相反,覆蓋「onActivityCreated」的方法,並做有...

@Override 
public void onActivityCreated(Bundle savedInstanceState) 
{ 
    mActivity = (BottomTabActivity) this.getActivity(); 
} 
0

編輯您的應用程序的AndroidManifest.xml文件,並在元素中添加以下聲明。這嵌入了應用程序編譯的Google Play服務版本。

<meta-data 
    android:name="com.google.android.gms.version" 
    android:value="@integer/google_play_services_version" /> 

- >這解決了我的問題。

3

您不能在XML中嵌套片段。你應該用代碼來做。 最好的方法是創建一個FrameLayout,並在運行時用您的片段替換它。

假設XML如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/inquiryLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".InquiryFragment" > 

<FrameLayout 
    android:id="@+id/contentFrame" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" > 

</FrameLayout> 
</RelativeLayout> 

現在的代碼很簡單:

public class InquiryFragment extends Fragment { 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View v = inflater.inflate(R.layout.inquiry_fragment, container, false); 

     Plan1Fragment frag = new Plan1Fragment(); 
     getChildFragmentManager().beginTransaction() 
      .replace(R.id.contentFrame, frag).commit(); 

     return v; 
    } 

} 

就是這樣。

0

代替將xmlns:android="http://schemas.android.com/apk/res/android"放入線性佈局標籤中,請將其放在<com.google.android.gms.ads.AdView標籤中。如下圖所示:

<com.google.android.gms.ads.AdView 
 
     xmlns:ads="http://schemas.android.com/apk/res-auto" 
 
     android:id="@+id/adView1" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignParentBottom="true" 
 
     app:adSize="SMART_BANNER" 
 
     app:adUnitId="ca-app-pub-9766031373541061/3761995838" 
 
     app:loadAdOnCreate="true" 
 
     app:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" />

爲進一步協助此次訪問Link

相關問題