我想设置我的烘焙应用程序,以便对于平板电脑,它显示两个 Pane (2 个 fragment )布局:
fragment A = 左 Pane :包含 RecyclerView 中列出的配方步骤和 fragment B = 右 Pane :包含步骤详细信息
对于移动设备(布局 xml 设置为小于 w600dp),我只想显示 fragment A。
这是我的 Activity 中保存这两个 fragment 的代码:
public class RecipeActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe);
// Instantiate Fragment Manager in charge of fragments associated with this Activity
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
RecipeFragment recipeFragment = new RecipeFragment();
fragmentTransaction.add(R.id.recipe_container, recipeFragment).commit();
RecipeDetailsFragment recipeDetailsFragment = new RecipeDetailsFragment();
fragmentManager.beginTransaction().add(R.id.recipe_details_container, recipeDetailsFragment).commit();
}
}
}
应用程序在移动屏幕模拟器上运行时抛出此错误:
java.lang.IllegalArgumentException: No view found for id 0x7f070081 (com.example.bakingapp:id/recipe_details_container) for fragment RecipeDetailsFragment{a70b224 (264e9189-e051-400b-9899-ba42871c2f89) id=0x7f0700
但是,如果我删除将recipe_details_container 添加到事务管理器的代码,则该 fragment 将能够毫无问题地加载。
所以我的问题是:
- 谁能解释一下我需要在这个方法中编写什么代码来考虑平板电脑的情况?
- 我认为我的理解中遗漏了一些内容,一些详细说明会有所帮助。
最佳答案
因为容器R.id.recipe_details_container
在“移动”布局中不存在。在尝试添加详细信息 fragment 之前,您应该检查 View 是否存在
关于java - 添加两 Pane 平板电脑布局导致在移动设备中找不到 View (小于 w600dp),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60858692/