Android 未找到片段id的视图
我被这个错误弄糊涂了,我正在做的是尝试替换DetailActivity中的片段,这是DetailActivityFragment,它在单击时被QuickFacts片段替换Android 未找到片段id的视图,android,android-fragments,Android,Android Fragments,我被这个错误弄糊涂了,我正在做的是尝试替换DetailActivity中的片段,这是DetailActivityFragment,它在单击时被QuickFacts片段替换 public class DetailActivityFragment extends Fragment{ TextView textView; ImageView imageView; TextView details; Button button; public DetailAct
public class DetailActivityFragment extends Fragment{
TextView textView;
ImageView imageView;
TextView details;
Button button;
public DetailActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_detail, container, false);
Bundle getBundle = getActivity().getIntent().getExtras();
String name = getBundle.getString("NAME");
textView= (TextView) view.findViewById(R.id.bioname);
textView.setText(name);
imageView= (ImageView) view.findViewById(R.id.imageView);
imageView.setImageResource(getBundle.getInt("IMAGE"));
Log.v("test", "images are coming");
button= (Button) view.findViewById(R.id.button_quickfacts);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = new QuickFacts();
FragmentTransaction fragmentTransaction =
getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_quickfacts, fragment);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
}
我的快速事实片段
public class QuickFacts extends Fragment {
TextView detailText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_quick_facts, container, false);
Bundle getBundle = getActivity().getIntent().getExtras();
String details=getBundle.getString("DETAIL");
detailText= (TextView) container.findViewById(R.id.fragment_add);
detailText.setText(details);
return view;
}
}
fragment\u quick\u facts.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_quickfacts"
tools:context="com.example.rahul.famousbiography.fragments.QuickFacts">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"
android:id="@+id/fragment_add"
/>
</FrameLayout>
这里的容器是您的
视图组
,因此,容器没有您的faragment\u add
,fragment\u quick\u facts.xml现在只有它,它当前充当您的视图
。
所以请换衣服
detailText= (TextView) container.findViewById(R.id.fragment_add);
到
还有看
fragmentTransaction.replace(R.id.fragment_quickfacts, fragment);
您正在尝试替换fragment\u quickfacts
,它位于fragment\u quickfacts.xml
中,而不是fragment\u detail.xml
中。因此,也可以检查它,或者也可以发布您的fragment\u detail.xml
如果您不清楚,请仔细检查它是否有效,尝试清理并重新构建您的项目。或者发布堆栈跟踪,可能details
为空。检查它。fragment\u quickfacts是fragment\u quick\u facts.xml的框架布局,它是quickfacts片段的布局,在细节片段中,我有一个按钮被单击并替换为quickfacts fragmentpaste logacat请
detailText= (TextView) view.findViewById(R.id.fragment_add);
fragmentTransaction.replace(R.id.fragment_quickfacts, fragment);