-2

There are two fragments ChatFragment and CreateGroupFragment. When FloatingActionButton is selected in ChatFragment, CreateGroupFragment should come screen. But when click this button app closing.

this is my code

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

crash

--------- beginning of crash 11-29 16:36:43.823 2656-2656/com.example.asus.buddy E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.asus.buddy, PID: 2656 java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.support.design.widget.FloatingActionButton at com.example.asus.buddy.CreateGroupFragment.onActivityCreated(CreateGroupFragment.java:159) at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2363) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1442) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809) at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799) at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367) at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700) at 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) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Divyesh Kalotra
  • 204
  • 1
  • 2
  • 13
cagla
  • 1
  • 3
  • Please follow SO guidelines to form a question [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and [keypoints to enhance qulity of question](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) – Pavneet_Singh Nov 29 '17 at 15:40

2 Answers2

0

here you can use simple transitions while starting your fragment.

@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();

            }
        });

read more about adding fragment transition stackoverflow

Medium link

Using shared elements

vikas kumar
  • 10,447
  • 2
  • 46
  • 52
  • It didnt change. has same error which is app stopped. – cagla Nov 29 '17 at 16:15
  • RelativeLayout cannot be cast to android.support.design.widget.FloatingActionButton – cagla Nov 29 '17 at 17:07
  • problem is with Fab button you are getting problem because you are casting Relative layout into Floating button please corrent below line and check again FloatingActionButton fab=(FloatingActionButton)getActivity().findViewById(R.id.addgroup); – vikas kumar Nov 29 '17 at 17:24
  • i advice you to intialize Flaoting action button in onCreateView() and set onClick listener there only – vikas kumar Nov 29 '17 at 17:27
  • I try it in onCreateView , it give error FloatingActionButton fab = (FloatingActionButton)getActivity().findViewById(R.id.addgroup); – cagla Nov 29 '17 at 17:35
  • dont use getActivity, use view object instead which you will be returning in onCreateView() – vikas kumar Nov 29 '17 at 17:36
  • FloatingActionButton fab = (FloatingActionButton)view.findViewById(R.id.addgro‌​up) – vikas kumar Nov 29 '17 at 17:37
  • can you just add your code here in question on which fragment its happening – vikas kumar Nov 29 '17 at 17:41
0

The ID you are referencing is wrong and that of a RelativeLayout, therefore the casting issue. Have a look at the exact FAB id, currently R.id.addgroup is of your layout and not your FAB.

Dimitar
  • 4,402
  • 4
  • 31
  • 47