2012-08-07 64 views
0

共同片段被用在2活動的使用FragmentActivity拋出InflateException

public class Ads extends Fragment { 
    private View rootView ; 
    private MoPubView adView ; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     rootView = inflater.inflate(R.layout.ads, container, true) ; 
     adView = (MoPubView) rootView.findViewById(R.id.adView) ; 
     adView.setAdUnitId(LogoQuizUtil.MOPUB_AD_UNIT);  
     adView.loadAd(); 
     return rootView; 
    } 
} 

片段佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/ad_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:gravity="bottom" > 

    <com.mopub.mobileads.MoPubView 
     android:id="@+id/adView" 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:layout_marginTop="10dp" 
     android:gravity="bottom" />  
</LinearLayout> 

其他佈局,其中包括我這個片段

​​

我得到一個不明白第一次調用setContentView(R.layout.ads)時發生異常,第二次調用時調用fragementactivity(Ads)我在setContentView(R.id.ads)處得到一個異常。

例外,我得到的是

08-07 09:38:30.359: W/System.err(975): java.lang.RuntimeException: Unable to start activity ComponentInfo{mypackage/mypackage.SecondActivity}: android.view.InflateException: Binary XML file line #263: Error inflating class fragment

+0

顯示您的片段的代碼。 – Barak 2012-08-07 04:37:08

+0

其上廣告延伸FragmentActivity – Siddharth 2012-08-07 04:42:47

+0

然後你需要研究片段。這是**不是**一個片段。這是支持庫中的一個類,用於創建可以使用片段的活動。片段將擴展片段類。 – Barak 2012-08-07 04:53:20

回答

3

讓我們從頭開始。活動不是片段,片段不是活動。

您需要一個活動來包含/控制片段。要做到這一點,請擴展Activity(用於Honeycomb +開發)或使用支持庫並擴展FragmentActivity。在這個類中,您將使用setContentView來設置將包含片段的佈局。

然後,您可以從該類中調用片段管理器(getFragmentManagergetSupportFragmentManager,具體取決於您擴展的類別)。片段管理器然後用於創建,附加和分離片段。

該片段不是活動,編碼方式不同。它應該有一個onCreateView方法,用於擴展片段的佈局並將其從活動返回給片段管理器。

然後,通常使用onActivityCreated作爲其餘代碼(或其他任何需要的)。

使更改正確使用碎片可能會或可能無法解決您的所有問題,因爲我看到您在佈局中使用某種自定義小部件,並且問題也可能存在。

+0

我會在一段時間內用正確的代碼更新答案。謝謝。 – Siddharth 2012-08-07 08:10:23

相關問題