【问题标题】:Android fragment is not workingAndroid 片段不工作
【发布时间】:2011-12-29 00:04:55
【问题描述】:

我正在尝试在 android 中实现一个简单的片段。

我尝试了各种方法,唯一没有出现错误的情况是我从布局中完全删除了片段。除此之外,我删除了 inflate 并尝试手动创建布局,但也没有用。

仅从布局中删除 fragment 似乎会使应用程序加载,无论我做什么,应用程序都会崩溃。

我不断收到以下错误:

12-29 04: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.IllegalArgumentException:二进制 XML 文件第 9 行:重复 id 0x7f090023,标签 null,或父 id 0x0 与另一个片段 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;
    }
}

在我的活动中,我没有做任何特别的事情,我尝试使用常规的 ActivityFragmentActivity,但我遇到了同样的问题。

任何帮助将不胜感激。

【问题讨论】:

    标签: java android android-fragments


    【解决方案1】:

    我自己从来没有对这种方法感到很幸运。另外,如果您想在布局中的同一位置实例化多种类型的片段,或者用手指(通过ViewPager)滑动浏览多个片段实例等,无论如何您都必须采取不同的方式.

    这就是我要做的:1) 使用FrameLayout 为片段保留空间。

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

    2) 使用以下 Java 代码实例化 Fragment:

    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”属性。如果你只有一个 Fragment 实例,那么你可以随意调用它;否则最好将标签名称指定为与该特定实例出现的内容相关的名称。

    不要忘记最后的commit()部分,否则将无法正常工作!

    【讨论】:

    • 老兄,花了 5 个小时尝试解决这个“随便”的问题,而您的解决方案在 1 秒内解决了它!喜欢它!
    【解决方案2】:

    如果您想与您的片段进行交互(甚至可能不这样做),您的活动需要是片段活动。

    您收到此错误的原因是 TableRow 不是有效的父级。您需要使用某种 ViewGroup 布局。我不知道你为什么在这里使用 tablerow,但你不应该使用它。尝试使用水平线性布局。如果目标是均匀间距,请在 xml 中为所有内容设置布局权重 1。

    否则你发布的内容看起来没问题...

    【讨论】:

    • 感谢您的帮助,但我确实尝试将 TableLayout 替换为简单的 TextView,但也没有用。我将尝试使用简单的FrameLayoutTextView 看看是否有帮助。
    猜你喜欢
    • 1970-01-01
    • 2021-08-09
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 2016-12-29
    • 2020-09-17
    • 2013-10-12
    相关资源
    最近更新 更多