Must specify unique android:id, android:tag, or have a parent with an id for XXX

把app的targetSdkVersion和compileSdkVersion升级到23以后,在进入某些页面的时候,出现了崩溃:

 Caused by: java.lang.IllegalArgumentException: Binary XML file line #49: Must specify unique android:id, android:tag, or have a parent with an id for com.xxx.XXXFragment

对应的布局文件如下:

 <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.haodf.prehospital.doctorhome.DoctorExperienceFragment"/>

就是一个Fragment的标签,那问题出在哪里呢?
官网中对于FragmentActivity的描述中,有这么一句:

When using the tag, this implementation can not use the parent view’s ID as the new fragment’s ID. You must explicitly specify an ID (or tag) in the .

意思就是呢,你要是用标签的话呢,你得显示的给他指定一个id或是tag。FragmentManager的源码也验证了这一点:

 @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        if (!"fragment".equals(name)) {
            return null;
        }

        String fname = attrs.getAttributeValue(null, "class");
        TypedArray a =  context.obtainStyledAttributes(attrs, FragmentTag.Fragment);
        if (fname == null) {
            fname = a.getString(FragmentTag.Fragment_name);
        }
        int id = a.getResourceId(FragmentTag.Fragment_id, View.NO_ID);
        String tag = a.getString(FragmentTag.Fragment_tag);
        a.recycle();

        if (!Fragment.isSupportFragmentClass(mHost.getContext(), fname)) {
            // Invalid support lib fragment; let the device's framework handle it.
            // This will allow android.app.Fragments to do the right thing.
            return null;
        }

        int containerId = parent != null ? parent.getId() : 0;
        if (containerId == View.NO_ID && id == View.NO_ID && tag == null) {
            throw new IllegalArgumentException(attrs.getPositionDescription()
                    + ": Must specify unique android:id, android:tag, or have a parent with an id for " + fname);
        }

        ......

        return fragment.mView;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值