23

I created a very simple fragment to test my app and I got the following error message:

03-31 16:04:39.834: E/AndroidRuntime(7860): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.team3.domore/com.team3.domore.TabActivity}: java.lang.ClassCastException: com.team3.domore.SomeFrag cannot be cast to android.support.v4.app.Fragment

My fragment is really simple...

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SomeFrag extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.alarm_frag, container, false);
    }

    @Override
    public void onStart() {
        super.onStart();
    }
}

Please help...I've been struggling with this for almost two hours..

EDIT: I'm pretty sure where I called this fragment (an activity that extends FragmentActivity) is working... just this fragment part is not working...

user1447343
  • 1,417
  • 4
  • 18
  • 24

2 Answers2

74

Your SomeFrag extends

android.app.Fragment

as stated in the imports. Change the import to

android.support.v4.app.Fragment

and the cast will succeed.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • 1
    Weird, I know this post was old. But for a newer version of android studio it was the opposite. I changed android.support.v4.app.Fragment to android.app.Fragment – beastlyCoder Feb 11 '17 at 05:02
  • I had the same issue but doing as u said did remove the error but my app stated crashing for some reason.. – Rishav Jun 10 '17 at 08:31
  • Why is android studio creating `android.app.fragment` instead of `android.support.v4.app.Fragment` by default? – kks21199 Aug 14 '18 at 15:00
17

try changing following import

import android.app.Fragment

to import android.support.v4.app.Fragment

minhaz
  • 4,233
  • 3
  • 33
  • 49