Android的碎片不工作 [英] Android fragment is not working

查看:177
本文介绍了Android的碎片不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的android简单的片段。

I'm trying to implement a simple fragment in android.

我已经试过各种方法,也是唯一一次,我没有得到一个错误,当我从布局中删除片段干脆。除此之外,我已经去充气,并试图手动创建一个布局和没有任何工作。

I've tried various approaches and the only time I don't get an error is when I remove the fragment from the layout altogether. Other than that, I've removed inflate and tried to create a layout manually and that didn't work either.

只有删除片段从布局似乎使应用负载,不管还有什么我做什么,应用程序崩溃。

Only removing the fragment from the layout seems to make the application load, no matter what else I do, the application crashes.

我不断收到以下错误:

4月12日至29日:52:07.493:E / AndroidRuntime(11717):
  了java.lang.RuntimeException:无法恢复活动
  {com.p5sys.android.jump / com.p5sys.android.jump.lib.activities.DisplayContactList}:
  android.view.InflateException:二进制XML文件行#9:错误
  充气类片段

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

我也是上面的错误堆栈跟踪(公正地分享柜面它帮助)后,收到以下错误。

I also get the following error after the above errors stacktrace (just sharing incase it helps).

4月12日至29日:52:07.493:E / AndroidRuntime(11717):产生的原因:
  java.lang.IllegalArgumentException异常:二进制XML文件行#9:重复
  ID 0x7f090023,标记为空,或父母ID为0x0与另一个片段上的
  com.p5sys.android.jump.lib.fragment.HeaderFragment

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 是一个片段的ListView 在里面:

My layout contact_layout.xml is a LinearLayout with a fragment and ListView in it:


<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;
    }
}

在我的活动我没有做什么特别的事情,我已经使用常规的活动试过以及 FragmentActivity 我已经得到了同样的问题。

In my Activity I'm not doing anything special, I've tried using regular Activity as well as FragmentActivity and I've gotten the same problem.

任何帮助将是非常美联社preciated。

Any help would be much appreciated.

推荐答案

我从未有过多少运气,这种方法我自己。另外,你将不得不采取不同的方式无论如何,如果你想的可能实例许多类型的片段在布局内的同一地点,或通过用手指(通过ViewPager)等片段的许多实例刷卡

I never had much luck with this approach myself. Plus, you're going to have to do it differently anyway if you want the possibility to instantiate many types of fragments in the same place within the layout, or swipe through many instances of Fragments with your finger (via ViewPager), etc.

因此​​,这里是我做的:1)使用的FrameLayout预留空间的片段

So here's what I do: 1) Use a FrameLayout to reserve space for the Fragment.

<FrameLayout
       android:id="@+id/where_i_want_my_fragment"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       />

2)用下面的Java code实例化片段:

2) Instantiate the Fragment with the following Java code:

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是你需要拿出一个字符串值。这是你要分配给分片的实例,类似于HTML中的id属性标签名称。如果你只有一个片段实例,然后你可以调用它任何你想要的;否则,最好指定标签名,涉及到什么具体的实例中出现的东西。

myFragTag is a String value you need to come up with. It's the tag name you want to assign to the Fragment instance, similar to the "id" attribute in HTML. If you only have one Fragment instance, then you can call it anything you want; otherwise it's best to designate the tag name as something related to what that specific instance is appearing in.

不要忘了提交()的一部分在最后,否则将无法正常工作!

Don't forget the commit() part at the very end, otherwise it won't work!

这篇关于Android的碎片不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆