2

I'm just getting started on learning fragments and this error is really frustrating me, I must be missing something simple:

I get an error on launch saying that the Fragment class cannot be cast to android.app.Fragment - I'm not sure why

xml for Activity:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/background_blue" > <Button android:id="@+id/bStartTest" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_marginBottom="16dp" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:background="@drawable/btn_main" android:text="@string/start_test" android:textColor="@color/white" android:textSize="24sp" /> <ProgressBar android:id="@+id/pbGetDataBase" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:visibility="invisible" /> <TextView android:id="@+id/tvHomeTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:padding="16dp" android:text="@string/app_name" android:textColor="@color/white" android:textSize="34sp" /> <LinearLayout android:id="@+id/firstTimeLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/tvHomeTitle" android:background="@color/background_blue" android:orientation="vertical" android:paddingLeft="16dp" android:paddingRight="16dp" > <fragment android:id="@+id/overviewFragment" android:layout_width="match_parent" android:layout_height="fill_parent" class="com.mycqs.cardiology.OverviewFragment" /> </LinearLayout> </RelativeLayout> 

Activity class:

public class HomeActivity extends SherlockActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); } } 

Fragment class:

public class OverviewFragment extends SherlockFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.temporary, container, false); return view; } } 

--

06-28 22:27:48.077: E/AndroidRuntime(23806): FATAL EXCEPTION: main 06-28 22:27:48.077: E/AndroidRuntime(23806): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycqs.cardiology/com.mycqs.cardiology.HomeActivity}: android.view.InflateException: Binary XML file line #87: Error inflating class fragment 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246) 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296) 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.app.ActivityThread.access$700(ActivityThread.java:151) 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.os.Handler.dispatchMessage(Handler.java:99) 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.os.Looper.loop(Looper.java:137) 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.app.ActivityThread.main(ActivityThread.java:5293) 06-28 22:27:48.077: E/AndroidRuntime(23806): at java.lang.reflect.Method.invokeNative(Native Method) 06-28 22:27:48.077: E/AndroidRuntime(23806): at java.lang.reflect.Method.invoke(Method.java:511) 06-28 22:27:48.077: E/AndroidRuntime(23806): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 06-28 22:27:48.077: E/AndroidRuntime(23806): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 06-28 22:27:48.077: E/AndroidRuntime(23806): at dalvik.system.NativeStart.main(Native Method) 06-28 22:27:48.077: E/AndroidRuntime(23806): Caused by: android.view.InflateException: Binary XML file line #87: Error inflating class fragment 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710) 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752) 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.view.LayoutInflater.inflate(LayoutInflater.java:495) 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 06-28 22:27:48.077: E/AndroidRuntime(23806): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327) 06-28 22:27:48.077: E/AndroidRuntime(23806): at com.actionbarsherlock.internal.ActionBarSherlockNative.setContentView(ActionBarSherlockNative.java:134) 06-28 22:27:48.077: E/AndroidRuntime(23806): at com.actionbarsherlock.app.SherlockActivity.setContentView(SherlockActivity.java:229) 06-28 22:27:48.077: E/AndroidRuntime(23806): at com.mycqs.cardiology.HomeActivity.onCreate(HomeActivity.java:58) 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.app.Activity.performCreate(Activity.java:5250) 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210) 06-28 22:27:48.077: E/AndroidRuntime(23806): ... 11 more 06-28 22:27:48.077: E/AndroidRuntime(23806): Caused by: java.lang.ClassCastException: com.mycqs.cardiology.OverviewFragment cannot be cast to android.app.Fragment 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.app.Fragment.instantiate(Fragment.java:585) 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.app.Fragment.instantiate(Fragment.java:560) 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.app.Activity.onCreateView(Activity.java:4850) 06-28 22:27:48.077: E/AndroidRuntime(23806): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686) 06-28 22:27:48.077: E/AndroidRuntime(23806): ... 23 more 
5
  • Have you cleaned the project? Are there any android.app.Fragment imports anywhere? Commented Jun 28, 2013 at 21:20
  • Please edit your question and paste in the entire stack trace. Commented Jun 28, 2013 at 21:22
  • Yes cleaned it and nope there aren't any android.app.Fragment imports. My minimum api level is set to 8, will this cause the error even though I am using the support package? Commented Jun 28, 2013 at 21:24
  • I can't seem to save the logcat to get the trace, the log.txt file is coming up as empty - any other methods to copy stack trace? Commented Jun 28, 2013 at 21:31
  • "I can't seem to save the logcat to get the trace" -- highlight the lines, then press Ctrl-C to copy them to the clipboard. Commented Jun 28, 2013 at 21:34

1 Answer 1

2

Change SherlockActivity to SherlockFragmentActivity. If you are going to use SherlockFragment, SherlockListFragment, etc., you need to host them in a SherlockFragmentActivity.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.