-1

My page is activity page and now i would like to change to fragment but it crashed,

Caused by: java.lang.ClassCastException: com.mac.Activity cannot be cast to android.app.Activity

I dont know which part of code is crashed. So I put my code in below.

Code:

public class Activity extends Fragment {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    final View view = inflater.inflate(R.layout.activity, container, false);

    final TextView nameTxt = (TextView) view.findViewById(R.id.nameTxtDetail);
    final TextView descTxt = (TextView) view.findViewById(R.id.descDetailTxt);

    final Button btn = (Button) view.findViewById(R.id.btn);
    //RECEIVE
    Intent i = getActivity().getIntent();
    String name = i.getExtras().getString("NAME_KEY");
    String desc = i.getExtras().getString("DESCRIPTION_KEY");

    //BIND
    nameTxt.setText(name);
    descTxt.setText(desc);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    getContext());
            alertDialogBuilder.setTitle("Do you want to login?");

            // set dialog message
            alertDialogBuilder
                    .setCancelable(false)
                    .setNeutralButton("YES", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            Intent i = new Intent(getActivity(), FacebookLogin.class);
                            startActivity(i);

                        }

                    })
                    .setPositiveButton("NO", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // if this button is clicked, close
                            // current activity
                            dialog.cancel();
                        }
                    });


            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();


        }
    });
    return view;
}


@Override
public void onResume() {
    super.onResume();
    ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
    TextView toolbar_title = (TextView) getActivity().findViewById(R.id.toolbar_title);
    toolbar_title.setText("DETAIL");

}

@Override
public void onStop() {
    super.onStop();
    ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(false);
}

}

First I though crashed because forget put final for textview and button but i already put now, still crashed. I am using android studio. Hope somebody help thanks.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Lolly Lun
  • 55
  • 1
  • 10
  • 2
    can you share the crash log? – Gregorius Giovanni Roswin Aug 24 '17 at 01:43
  • make sure you are passing bundle to this intent as u are trying to access NAME_KEY. put your crash log here. – Palak Darji Aug 24 '17 at 01:45
  • 1
    Caused by: java.lang.ClassCastException: com.mac.Activity cannot be cast to android.app.Activity – Lolly Lun Aug 24 '17 at 01:47
  • 1
    First. you should not name your fragment as "Activity". Second. can u tell us which line is it pointing to? Third. If u are supposed to pass bundle to fragment from where it is called, then here in fragment you must use getArguments() to access it. – Palak Darji Aug 24 '17 at 01:51
  • Let me know one thing that the class `FacebookLogin` is fragmet or activity? – Mable John Aug 24 '17 at 02:01
  • FacebookLogin is activity – Lolly Lun Aug 24 '17 at 02:11
  • Poor naming convention make it hard for you and us debug your application. Suffix all Fragments with Fragment, and all Activities with Activity. `Activity extends Fragment` is just really bad as you're shadowing the builtin Activity class – OneCricketeer Aug 24 '17 at 03:07
  • Please read https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this for tips on how to find the reason for the crash. – Code-Apprentice Aug 24 '17 at 03:08

2 Answers2

0

There are a lot of factors here that could cause this. But firstly did you remove it from Android Manifest, because fragments are not shown in Android Manifest, and if you didn't it would try opening it as an activity.

Tawanda Muzavazi
  • 405
  • 3
  • 14
-2

Override another method onViewCreated(). Get all the codes you put in onCreateView(), except the first line that inflates, put them in onViewCreated(). This way you are sure the view has been created and is ready to be used.

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

        @Override
        public void onViewCreated (View view, Bundle savedInstanceState){
super.onViewCreated (view, savedInstanceState);
        final TextView nameTxt = (TextView) view.findViewById(R.id.nameTxtDetail);
         final TextView descTxt = (TextView) view.findViewById(R.id.descDetailTxt);
         final Button btn = (Button) view.findViewById(R.id.btn); 

        //RECEIVE 
        Intent i = getActivity().getIntent(); 
        String name = i.getExtras().getString("NAME_KEY");
         String desc = i.getExtras().getString("DESCRIPTION_KEY"); 

        //BIND
         nameTxt.setText(name);
         descTxt.setText(desc);
         btn.setOnClickListener(new View.OnClickListener() { 
        @Override 
    public void onClick(View view) { 
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( getContext());
         alertDialogBuilder.setTitle("Do you want to login?"); 
        // set dialog message alertDialogBuilder .setCancelable(false)
         .setNeutralButton("YES", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int id) { 
        Intent i = new Intent(getActivity(), FacebookLogin.class); startActivity(i); 
        } 
        })
         .setPositiveButton("NO", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int id) { 
        // if this button is clicked, close //
         current activity dialog.cancel(); 
        }
         }); 

        // create alert dialog
         AlertDialog alertDialog = alertDialogBuilder.create(); 
        // show it alertDialog.show(); 
        }
         });
        }
Francis Nduba Numbi
  • 2,499
  • 1
  • 11
  • 22
  • This is the exact same code, just a different approach to write it. See the comments above for the actual problem – OneCricketeer Aug 24 '17 at 03:09
  • This approach is better as it ensures that the view is ready for use. It can crash as you're trying to access a view which isn't laid yet. – Francis Nduba Numbi Aug 24 '17 at 03:13
  • I've never come across an instance where `final View view = inflater.inflate` "isn't laid yet" – OneCricketeer Aug 24 '17 at 03:20
  • It is laid when this method returns – Francis Nduba Numbi Aug 24 '17 at 03:23
  • void onViewCreated (View view, Bundle savedInstanceState) Called immediately afteronCreateView(LayoutInflater, ViewGroup, Bundle) has returned, but before any saved state has been restored in to the view. This gives subclasses a chance to initialize themselves once they know their view hierarchy has been completely created. The fragment's view hierarchy is not however attached to its parent at this point. – Francis Nduba Numbi Aug 24 '17 at 03:31
  • I understand your points, but findViewById only requires an inflated view, not an attached one. – OneCricketeer Aug 24 '17 at 18:45