我在运行下面的No view found for id for fragment EHRFragment
代码时收到错误MainActivity.java
。我搜索了这个问题的所有回答,找不到能解决我问题的问题。在这个程序中,我正在为平板电脑和手机实现导航抽屉。它认为它没有识别布局文件(即使我有一个)。
以下是MainActivity.java
的代码:
epublic class MainActivity extends AppCompatActivity {
protected boolean isTablet = false;
protected boolean isPortrait = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isTablet = getResources().getBoolean(R.bool.isTablet);
isPortrait = getResources().getBoolean(R.bool.isPortrait);
if(savedInstanceState==null) {
if(isTablet){
showFragment(new EHRFragment(), true, true, EHRFragment.class.getSimpleName());
showFragment(new ProtocolsFragment(), false, true, ProtocolsFragment.class.getSimpleName());
} else {
showFragment(new EHRFragment(), true, true, EHRFragment.class.getSimpleName());
}
}
}
public void showFragment(Fragment fragment, boolean primary, boolean addToBackStack, String backStackTag) {
if(fragment == null) {
return;
}
FragmentTransaction lft = getSupportFragmentManager().beginTransaction();
if (isTablet) {
if (primary) {
if (addToBackStack) {
lft.replace(R.id.leftFragmentContainer, fragment).addToBackStack(backStackTag).commit();
} else {
lft.replace(R.id.leftFragmentContainer, fragment).commit();
}
} else {
if (addToBackStack) {
lft.replace(R.id.rightFragmentContainer, fragment).addToBackStack(backStackTag).commit();
} else {
lft.replace(R.id.rightFragmentContainer, fragment).commit();
}
}
} else {
if (addToBackStack) {
lft.replace(R.id.fragmentContainer, fragment).addToBackStack(backStackTag).commit();
} else {
lft.replace(R.id.fragmentContainer, fragment).commit();
}
}
}
}
以下是手机上显示的content_view.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal">
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
此处是ehr.xml
的一部分,应显示EHRFragment.java
:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</ScrollView>