2012-02-07 35 views
0

所以我有一個活動有一個FragmentPagerAdapter和每個片段裏面我有兩個其他碎片(這裏稱爲a和b)。片段a是一個ListFragment,而片段b只是顯示該值的普通片段。Android碎片行爲不如預期

這裏有兩個問題...

1)我不能搶在片段B

2)從活動的看法。如果我註釋掉的爭奪,而不是顯示在片段的電話一旦選擇了一個項目後,B就會顯示在同一個viewpager上。

但是,我似乎無法正確加載我的佈局。但是,如果我刪除b一切似乎很好地工作......

的活動加載以下佈局...

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <com.viewpagerindicator.TitlePageIndicator 
     android:id="@+id/indicator" 
     android:padding="10dip" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     /> 
    <android.support.v4.view.ViewPager 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/mainpager"/> 
</LinearLayout> 

我的第一塊碎片看起來如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" > 

    <fragment android:name="com.gleason.apa.fragment.BarListFragment" 
      android:id="@+id/bar_list_fragment" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" /> 
    <fragment android:name="com.gleason.apa.fragment.BarEditFragment" 
      android:id="@+id/bar_edit_fragment" 
      android:layout_weight="2" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" /> 
</LinearLayout> 

片段A

​​

Fragement B

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

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Bar Name" /> 
     <EditText 
    android:id="@+id/bar_name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:inputType="text" 
     android:hint="Name" 
     /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Address" /> 

     <EditText 
      android:id="@+id/bar_address" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:inputType="text" 
      android:hint="Address"/> 
    </LinearLayout> 
<Button 
    android:id="@+id/submitBar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Add Game"/> 
</LinearLayout> 

然而,當我嘗試編譯和運行,我得到以下...

android.view.InflateException: Binary XML file line #6: Error inflating class fragment 
java.lang.NullPointerException 

任何想法?

UPDATE:

所以,空指針異常來自BarEditFragment ...

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    .... 
    name = (EditText) activity.findViewById(R.id.bar_name); 
    address = (EditText) activity.findViewById(R.id.bar_address); 
Button submit = (Button) activity.findViewById(R.id.submitBar); 
} 

所有那些回來空。

此外,如果我刪除這些,碎片即使在我的手機上也會顯示。所以它並排顯示兩個小碎片,而不是像它應該的2個「屏幕」。

而且這並不工作...

activity.getFragmentManager().findFragmentById(R.id.bar_edit_fragment).getView().findViewById(R.id.bar_name); 

回答

0

我失蹤的事情是,你需要創建佈局,告訴它是否是單或雙窗格。解決這個問題的最好方法是使用NewsReader示例作爲模板。