2012-05-31 61 views
0

我有一個基於tabHost應用程序,與片段, 它,如果我用我的片段與LinearLayout中listFragment不充氣基於tabHost應用

工作正常,但我需要對選項卡視圖之一[列表片段]

但應用程序崩潰,因爲不正確地充氣: 我的類[標籤視圖片段]:

public class Tab2Fragment extends Fragment { //primera pregunta, lo puedo hacer asi o con FragmentActivity 
//public class Tab2Fragment extends ListFragment { //primera pregunta, lo puedo hacer asi o con FragmentActivity 


    private FragmentManager fm; 
    private ListFragment list; 
    private List<Map<String, String>> listItems; 
    private String[] froms; 
    private int[] viewIds; 

    /* (non-Javadoc) 
    * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) 
    */ 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     if (container == null) { 
      // We have different layouts, and in one of them this 
      // fragment's containing frame doesn't exist. The fragment 
      // may still be created from its saved state, but there is 
      // no reason to try to create its view hierarchy because it 
      // won't be displayed. Note this is not needed -- we could 
      // just run the code below, where we would create and return 
      // the view hierarchy; it would just never be used. 
      return null; 
     } 
     return (LinearLayout)inflater.inflate(R.layout.tab_frag2_layout, container, false); 
    } 



} 

我的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#00FF00"> 

     <fragment 
     android:name="android.support.v4.app.ListFragment" 
     android:id="@+id/List" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"/> 

</LinearLayout> 

錯誤:

05-31 22:39:58.412: E/AndroidRuntime(2796): FATAL EXCEPTION: main 
05-31 22:39:58.412: E/AndroidRuntime(2796): android.view.InflateException: Binary XML file line #8: Error inflating class fragment 
05-31 22:39:58.412: E/AndroidRuntime(2796):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:582) 
05-31 22:39:58.412: E/AndroidRuntime(2796):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618) 
05-31 22:39:58.412: E/AndroidRuntime(2796):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407) 
05-31 22:39:58.412: E/AndroidRuntime(2796):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
05-31 22:39:58.412: E/AndroidRuntime(2796):  at com.orchard.elasto.Tab2Fragment.onCreateView(Tab2Fragment.java:54) 
05-31 22:39:58.412: E/AndroidRuntime(2796):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870) 

那麼,如何正確地充氣名單?

我應該這樣做fragmentList,即使當tabHost指向一個片段?

非常感謝!

+0

爲什麼你沒有擴展你Tab2Fragment從ListFragment並使用這種方式? –

回答

1

你的問題是在這裏:

android:name="android.support.v4.app.ListFragment" 

你告訴它運行該片段是ListFragment類。

您需要將其更改爲引用您的類,並且您需要使該類成爲ListFragment而不是Fragment。

android:name="your.package.name.Tab2Fragment"