我通过onclick按钮替换我的片段但是得到这个错误plz帮助 这是我的详细活动片段
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.quickfacts);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = new QuickFacts();
FragmentTransaction fragmentTransaction =
getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.quickfacts, fragment);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
}
这是我的qucikfacts.xml,它是quickfact片段的一部分,其中fragmeht在点击时替换
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/quickfacts"
></FrameLayout>
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
这是我的片段细节,我有那个按钮
<ImageButton
android:layout_width="150dp"
android:layout_height="250dp"
android:scaleType="centerCrop"
android:id="@+id/imageView"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/bioname" />
<Button
android:id="@+id/quickfacts"
android:text="Quick Facts"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:layout_width="150dp"
android:layout_height="60dp"
android:background="@drawable/quick_facts"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_below="@+id/imageView"
android:layout_alignParentStart="true" />
<Button
android:id="@+id/biodetail"
android:text="Biography"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:layout_width="150dp"
android:layout_height="60dp"
android:background="@drawable/quick_facts"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_below="@+id/imageView"
android:layout_alignParentEnd="true" />
<Button
android:id="@+id/exit"
android:text="Exit"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:layout_width="150dp"
android:layout_height="60dp"
android:background="@drawable/quick_facts"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_alignTop="@+id/quotes"
android:layout_alignStart="@+id/biodetail"
android:layout_alignParentEnd="true" />
<Button
android:id="@+id/quotes"
android:text="Quotes"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:layout_width="150dp"
android:layout_height="60dp"
android:background="@drawable/quick_facts"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_below="@+id/quickfacts"
android:layout_alignParentStart="true"
android:layout_marginTop="43dp" />
这是logcat
FATAL EXCEPTION: main
Process: com.example.rahul.famousbiography, PID: 723
java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.view.ViewGroup
at android.support.v4.app.BackStackRecord.configureTransitions(BackStackRecord.java:1132)
at android.support.v4.app.BackStackRecord.beginTransition(BackStackRecord.java:1039)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:658)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:1)
fragmentTransaction.replace(R.id.quickfacts, fragment);
您确定自己的片段在按钮内吗?
button= (Button) view.findViewById(R.id.quickfacts);
或者是您的按钮frame layout ?
我认为你们中的任何一个都错了。