没有找到id的片段
问题描述:
我需要帮助。我得到了Crashlytics的崩溃:没有找到id的片段
Fatal Exception: java.lang.IllegalArgumentException: No view found for id 0x7f0f016c (com.my.my:id/fragment_root_layout) for fragment a{2e8d61e1 #4 id=0x7f0f016c fragment_tag}
我使用片段进入片段。在父片段的OnCreate方法中,我使用帮助FragmentManager添加子片段。我有一个想法,问题是在OnCreate方法中添加子片段,并且需要在OnCreateView中执行此操作。但我不确定。事情是我不能再现这次崩溃,但我在Crashlytic上得到这个崩溃。 父级片段装载的所有视图都有一个ID。只有当我在OnCreateView方法中膨胀不正确的布局时,我才能重现此崩溃,但在实时构建中不会发生这种情况。 请给我一个建议。
编辑:添加子片段
准则@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LogUtils.verbose(TAG, "displayOverlayFragment: attempt to add a fragment using FragmentTransaction");
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
OverlayFragment overlayFragment = new OverlayFragment();
fragmentTransaction.add(R.id.fragment_root_layout, overlayFragment, FRAGMENT_TAG);
fragmentTransaction.commit();
}
代码onCreateView的:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(getLayoutResId(), container, false);
mRecyclerView = initRecyclerView(view);
mProgressBar = view.findViewById(R.id.progressBar);
mProgressBarLabel = (TextView) view.findViewById(R.id.progressBarLabel);
}
return view;
}
@Override
protected int getLayoutResId() {
return R.layout.fragment_favorites;
}
答
也许你要申报proguard-rules.pro东西在你的发布版本?你有没有检查它是否仅在发布版本中发生?我认为你的调试版忽略了proguard,它的工作正常,但如果你检查你的发布版本,你将有机会重现你的崩溃。也许一些片段子组件应该在proguard中声明。
+0
谢谢,它是有道理的。但我仍然无法重现它,会尝试更多。 – LeShChEnKoUa
答
对于android中的片段,您需要在onStart()方法中对它们进行编码。并使用onCreateView()返回你想要的特定片段的布局
你确定存在一个ID为'fragment_root_layout'的视图吗? –
尝试将子片段添加到父片段的“onViewCreated”方法中。在此之前,您不应使用父母版面中的任何ID。 – hibob
发布你的代码到目前为止你已经尝试过吗? – FAT