片段到片段转换与FloatingActionButton Android

时间:2017-11-29 15:36:55

标签: android android-layout android-studio android-fragments

ChatFragment和CreateGroupFragment有两个片段。在ChatFragment中选择FloatingActionButton时,CreateGroupFragment应该出现在屏幕上。但是当点击此按钮应用关闭时。

这是我的代码

public void onActivityCreated(@Nullable Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);

        FloatingActionButton fab=(FloatingActionButton)getActivity().findViewById(R.id.addgroup);
        fab.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                getFragmentManager().beginTransaction().add(R.id.content,new CreateGroupFragment(),
                        "Fragmentcreate").addToBackStack("Fragmentcreate").commit();

            }
        });

    }

this is chatFragment

enter image description here

碰撞

  

---------崩溃的开始11-29 16:36:43.823 2656-2656 / com.example.asus.buddy E / AndroidRuntime:FATAL EXCEPTION:   main流程:com.example.asus.buddy,PID:2656   java.lang.ClassCastException:android.widget.RelativeLayout不能   施放到android.support.design.widget.FloatingActionButton at   com.example.asus.buddy.CreateGroupFragment.onActivityCreated(CreateGroupFragment.java:159)   在   android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2363)   在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1442)   在   android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)   在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)   在   android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)   在   android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)   在   android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)   在   android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)   在   android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)   在   android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:700)   在android.os.Handler.handleCallback(Handler.java:789)at   android.os.Handler.dispatchMessage(Handler.java:98)at   android.os.Looper.loop(Looper.java:164)at   android.app.ActivityThread.main(ActivityThread.java:6541)at   java.lang.reflect.Method.invoke(Native Method)at   com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

2 个答案:

答案 0 :(得分:0)

这里你可以在启动片段时使用简单的过渡。

@Override
public void onClick(View v) {
getFragmentManager().beginTransaction().setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out).add(R.id.content,new CreateGroupFragment(),
                        "Fragmentcreate").addToBackStack("Fragmentcreate").commit();

            }
        });

详细了解如何添加片段转换stackoverflow

link

Using共享元素

答案 1 :(得分:0)

您引用的ID是错误的,而且是RelativeLayout的ID,因此是转换问题。看看确切的FAB ID,目前R.id.addgroup是您的布局,而不是您的FAB。