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