单击按钮时无法打开片段

时间:2015-06-19 14:05:36

标签: java android xml android-layout android-fragments

我创建了2个片段及其布局, 当我点击配置文件片段上的按钮时,它应该打开平面布局,但是 它强制停止应用并且不打开第二个片段, 我已经包含了片段代码及其布局代码..

  

profilefragment.java

public class ProfileFragment extends Fragment {
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.profile, container, false);

    Button button = (Button) rootView.findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            onButtonClicked(v);
        }
    });
    return rootView;
}
public void onButtonClicked(View view)
{
    //do your stuff here..
    final FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.button, new flatview(), "NewFragmentTag");
    ft.commit();
}
  

是profile.xml

<RelativeLayout 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"
tools:context="${packageName}.${activityClass}"
android:id="@+id/semester">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

  

flatview.java

public class flatview extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.flat, container, false);
}
  

flat.xml

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="second fragement"
    android:id="@+id/textView3"
    android:layout_gravity="center_horizontal" />

logcat的

06-19 07:57:56.945  26457-26457/com.idealdeveloper.saltechnical E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.idealdeveloper.saltechnical, PID: 26457
java.lang.ClassCastException: android.widget.Button cannot be cast to android.view.ViewGroup
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:917)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
        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:5221)
        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:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

3 个答案:

答案 0 :(得分:0)

试试这个我想它会对你有用

 <RelativeLayout 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"
 tools:context="${packageName}.${activityClass}"
android:id="@+id/semester">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/fragmentLayout"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
 <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

public class ProfileFragment extends Fragment {
  View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {

rootView = inflater.inflate(R.layout.profile, container, false);

Button button = (Button) rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        onButtonClicked(v);
    }
});
return rootView;
 }
  public void onButtonClicked(View view)
  {
   //do your stuff here..
   final FragmentTransaction ft = getFragmentManager().beginTransaction();
   ft.add(R.id.fragmentLayout, new flatview(), "NewFragmentTag");
   ft.commit();
   }

我希望这个会帮助你

答案 1 :(得分:0)

片段事务将View插入ViewGroup,因此您不能只替换Button。您只需替换父semester,因为它是ViewGroup

public void onButtonClicked(View view) {
    final FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.semester, new flatview(), "NewFragmentTag");
    ft.commit();
}

如果你打算删除按钮,你需要手动完成,因为FragmentManager只能修改运行时事务:

ViewGroup parent = (ViewGroup) getActivity().findViewById(R.id.semester);
parent.removeView(view);

答案 2 :(得分:-1)

您必须为flat.xml布局提供$("#refresh_gridtable").bind("click", function(){ $("#gridtable").jqGrid("setGridParam", {datatype: 'json'}); return [true]; }); 根,例如,您可以使用ViewGroup

围绕TextView

flat.xml:

LinearLayout