2011-12-29 75 views
0

我想在android中實現一個簡單的片段。Android片段不工作

我試過各種方法,唯一一次我沒有得到一個錯誤是當我從佈局中完全刪除片段。除此之外,我已經刪除了充氣,並嘗試手動創建佈局,但這也不起作用。

只從版面中刪除fragment似乎使應用程序加載,無論我做了什麼,應用程序崩潰。

我不斷收到以下錯誤:

12-29 04:52:07.493: E/AndroidRuntime(11717): java.lang.RuntimeException: Unable to resume activity {com.p5sys.android.jump/com.p5sys.android.jump.lib.activities.DisplayContactList}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment

我也得到了上面的錯誤堆棧跟蹤後,下面的錯誤(只是分享櫃面它幫助)。

12-29 04:52:07.493: E/AndroidRuntime(11717): Caused by: java.lang.IllegalArgumentException: Binary XML file line #9: Duplicate id 0x7f090023, tag null, or parent id 0x0 with another fragment for com.p5sys.android.jump.lib.fragment.HeaderFragment

我的佈局contact_layout.xml是在其與fragmentListView一個的LinearLayout:

<fragment 
    android:id="@+id/headerFragment" 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    class="com.p5sys.android.jump.lib.fragment.HeaderFragment" /> 

<ListView 
    android:id="@+id/contactList" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:background="@color/transparent" 
    android:cacheColorHint="#00000000" > 
</ListView> 

片段佈局header_fragment.xml是:

<TableRow> 
    <ImageButton 
     android:id="@+id/btnSettings" 
     android:gravity="left" 
     android:hint="@string/label_settings" 
     android:onClick="btnAction_launchSettings" 
     android:padding="3dip" 
     android:src="@drawable/ic_menu_add" /> 

    <TextView 
     android:id="@+id/headerText" 
     android:gravity="center" 
     android:height="50dip" 
     android:padding="3dip" 
     android:textSize="20sp" 
     android:textStyle="bold" 
     android:text="Blank" /> 

    <ImageButton 
     android:id="@+id/btnAdd" 
     android:background="@drawable/disconnectbutton" 
     android:gravity="right" 
     android:hint="@string/label_add" 
     android:onClick="btnAction_launchAddNew" 
     android:padding="3dip" 
     android:src="@drawable/ic_menu_add" /> 
</TableRow> 

我的片段類:

package com.p5sys.android.jump.lib.fragment; 


import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

import com.p5sys.android.jump.lib.R; 
/*** 
* Fragment 
* @author Ali 
* 
*/ 
public class HeaderFragment extends Fragment { 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     // inflate layout 
     return inflater.inflate(R.layout.header_fragment, container, false); 
//  LinearLayout ll = new LinearLayout(getActivity()); 
//  TextView tv = new TextView(getActivity()); 
//  tv.setText("hello world"); 
//  ll.addView(tv); 
//  return ll; 
    } 
} 

在我的活動我沒有做什麼特別的事情,我已經使用常規Activity以及FragmentActivity試過,我已經得到了同樣的問題。

任何幫助將不勝感激。

回答

2

我自己從來沒有很好的運用這種方法。另外,如果您想要在佈局中的相同位置實例化許多類型的片段,或者用手指滑過片段的許多實例(通過ViewPager)等,您將必須以不同方式執行此操作。

這就是我所做的:1)使用FrameLayout爲片段預留空間。

2)實例化片段與下面的Java代碼:

FragmentManager fragMgr = getFragmentManager(); 
FragmentTransaction xact = fragMgr.beginTransaction(); 
if (null == fragMgr.findFragmentByTag(myFragTag)) { 
    xact.add(R.id.where_i_want_my_fragment, MyDerivedFragment.newInstance(), myFragTag).commit(); 
} 

myFragTag是你需要拿出一個字符串值。這是您想要分配給Fragment實例的標籤名稱,類似於HTML中的「id」屬性。如果你只有一個片段實例,那麼你可以任意調用它;否則最好將標籤名稱指定爲與特定實例出現的相關內容。

不要忘記commit()部分在最後,否則它將無法工作!

+0

老兄,試圖解決這個「無論」問題5個小時,你的解決方案在1秒內修復它!愛它! – Ali 2011-12-29 07:20:43

0

如果您想與您的片段進行交互(也許即使您不這樣做),您的活動也需要成爲片段活動。

你得到這個錯誤的原因是TableRow不是有效的父對象。你需要使用某種ViewGroup佈局。我不知道你爲什麼在這裏使用tablerow,但你不應該使用它。改爲嘗試水平線性佈局。如果目標是有均勻的間距,則給所有的佈局權重爲1 xml。

否則你發佈的內容看起來不錯...

+0

感謝您的幫助,但我嘗試用一​​個簡單的'TextView'替換'TableLayout',但這也沒有效果。我會嘗試一個簡單的'FrameLayout'和'TextView',看看它是否有幫助。 – Ali 2011-12-29 06:23:52