我正在創建具有兩個標籤的菜單。每個標籤都有三個片段顯示內容。我雖然這將是直截了當,但經過試驗和谷歌搜索,看起來像我錯了。標籤內的碎片
我設法創建片段到第一個選項卡。然而,當我切換到其他選項卡,回來了,我得到了以下錯誤消息和應用程序崩潰:
07-11 16:49:57.829: E/AndroidRuntime(20009): FATAL EXCEPTION: main
07-11 16:49:57.829: E/AndroidRuntime(20009): android.view.InflateException: Binary XML file line #8: Error inflating class fragment
07-11 16:49:57.829: E/AndroidRuntime(20009): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
07-11 16:49:57.829: E/AndroidRuntime(20009): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
07-11 16:49:57.829: E/AndroidRuntime(20009): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-11 16:49:57.829: E/AndroidRuntime(20009): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-11 16:49:57.829: E/AndroidRuntime(20009): at com.machinedata.ui.feedback.UsersFragment.onCreateView(UsersFragment.java:16)
07-11 16:49:57.829: E/AndroidRuntime(20009): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828)
07-11 16:49:57.829: E/AndroidRuntime(20009): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
07-11 16:49:57.829: E/AndroidRuntime(20009): at android.app.BackStackRecord.run(BackStackRecord.java:622)
07-11 16:49:57.829: E/AndroidRuntime(20009): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
07-11 16:49:57.829: E/AndroidRuntime(20009): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
07-11 16:49:57.829: E/AndroidRuntime(20009): at android.os.Handler.handleCallback(Handler.java:605)
07-11 16:49:57.829: E/AndroidRuntime(20009): at android.os.Handler.dispatchMessage(Handler.java:92)
07-11 16:49:57.829: E/AndroidRuntime(20009): at android.os.Looper.loop(Looper.java:137)
07-11 16:49:57.829: E/AndroidRuntime(20009): at android.app.ActivityThread.main(ActivityThread.java:4507)
07-11 16:49:57.829: E/AndroidRuntime(20009): at java.lang.reflect.Method.invokeNative(Native Method)
07-11 16:49:57.829: E/AndroidRuntime(20009): at java.lang.reflect.Method.invoke(Method.java:511)
07-11 16:49:57.829: E/AndroidRuntime(20009): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
07-11 16:49:57.829: E/AndroidRuntime(20009): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
07-11 16:49:57.829: E/AndroidRuntime(20009): at dalvik.system.NativeStart.main(Native Method)
07-11 16:49:57.829: E/AndroidRuntime(20009): Caused by: java.lang.IllegalArgumentException: Binary XML file line #8: Duplicate id 0x7f0b0013, tag null, or parent id 0xffffffff with another fragment for com.machinedata.ui.UserListFragment
07-11 16:49:57.829: E/AndroidRuntime(20009): at android.app.Activity.onCreateView(Activity.java:4252)
07-11 16:49:57.829: E/AndroidRuntime(20009): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
現在我的標籤是片段本身,這是動態放置到屏幕上。然後,這些片段將靜態地放入片段的xml文件加大。經過一些谷歌搜索後,我發現將碎片放入碎片中有點棘手。
getChildFragmentManager在API級別17中工作,但我想保持API級別爲14.有沒有辦法創建這種菜單結構?我需要爲選項卡創建單獨的活動嗎?
這裏是我的活動,顯示選項卡:
public class FeedbackActivity extends Activity implements UserListFragment.OnItemClickedListener
{
private ToplistsFragment toplist;
private UsersFragment users;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
setContentView(R.layout.feedback_screen);
//create tabs for dashboard and graphs
ActionBar.Tab tab1 = actionBar.newTab().setText(R.string.feedbackUsers);
ActionBar.Tab tab2 = actionBar.newTab().setText(R.string.feedbackToplist);
toplist = new ToplistsFragment();
users = new UsersFragment();
tab2.setTabListener(new MyTabsListener(toplist));
tab1.setTabListener(new MyTabsListener(users));
actionBar.addTab(tab1);
actionBar.addTab(tab2);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// TODO Auto-generated method stub
Log.v("log","ID: " + item.getItemId() + " " + item.getTitle());
switch (item.getItemId())
{
case android.R.id.home:
Intent intent = new Intent(this, ModeSelectActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onUserItemClick(AdapterView<?> parent, View v, int position, long id, UserData user)
{
//wite your activity code here
Log.v("log", "name: " + user.getName());
}
//tablistener
class MyTabsListener implements ActionBar.TabListener
{
public Fragment fragment;
public MyTabsListener(Fragment fragment)
{
this.fragment = fragment;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft)
{
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
ft.replace(R.id.feedback_fragcontainer, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft)
{
ft.remove(fragment);
}
}
}
它的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" >
<LinearLayout
android:id="@+id/feedback_fragcontainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</LinearLayout>
以及片片段的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="horizontal"
android:baselineAligned="false">
<fragment
android:id="@+id/feedbackuserListFragment"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="match_parent"
class="com.machinedata.ui.UserListFragment" >
</fragment>
<fragment
android:id="@+id/feedbackSessionListFragment"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="match_parent"
class="com.machinedata.ui.feedback.SessionListFragment" >
<!-- Preview: [email protected]/details -->
</fragment>
<fragment
android:id="@+id/feedbackDetailsFragment"
android:layout_width="match_parent"
android:layout_weight="0.5"
android:layout_height="match_parent"
class="com.machinedata.ui.feedback.DetailsFragment" >
<!-- Preview: [email protected]/details -->
</fragment>
</LinearLayout>
說實話,我我不確定錯誤信息與重複ID的含義是什麼,或者是什麼原因造成的。基本上我正在尋找一種解決方案來創建我描述的菜單結構或修復當前實現的方法。
該佈局似乎不是'UsersFragment'的'onCreateView()'膨脹。堆棧跟蹤報告提示''元素存在問題,並且您在此處發佈的佈局不包含此內容。你確定'onCreateView()'是膨脹正確的佈局? –
CommonsWare
而不是「正常」片段使用支持庫中的一個http://developer.android.com/reference/android/support/v4/app/Fragment.html#getChildFragmentManager() – Selvin
忘了添加該選項卡的xml文件。活動應該充氣feedback_screen.xml和UsersFragment(選項卡)應膨脹feedbackusers_tab.xml。 – Tumetsu