1

I'm trying to follow the Udacity Developing Android Apps course. I got to Lesson 3.03. Here, we are instructed to launch a new Activity (DetailActivity) from ForecastFragment when clicking an item in a list.

However, I keep getting an error when attempting to launch the DetailActivity. Binary XML file line #1: Error inflating class fragment, where it's caused by DetailActivity.java in it's onCreate() method.

I've already searched around, but none of the fixes suggested for this issue helped me at all.

The specific error is:

Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class
com.myapp.sunshine.app.DetailActivityFragment that is not a Fragment

DetailActivity.java

public class DetailActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail); //failing here
    }
}

DetailActivityFragment.java

public class DetailActivityFragment extends Fragment {

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

activity_detail.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment"
    android:name="com.myapp.sunshine.app.DetailActivityFragment"
    tools:layout="@layout/fragment_detail" android:layout_width="match_parent"
    android:layout_height="match_parent" />

I should mention that I'm not using support libraries--as I'm targeting SDK 21+, Lollipop only.

This isn't the only fragment we've created in the course either. The other fragment we created looks identical to this one.

activity_main.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment"
    android:name="com.myapp.sunshine.app.ForecastFragment"
    tools:layout="@layout/fragment_main" android:layout_width="match_parent"
    android:layout_height="match_parent" />

And for good measure, I'm including the ForecastFragment class. I've omitted unnecessary parts of code.

ForecastFragment.java

public class ForecastFragment extends Fragment {

    private ArrayAdapter<String> mForecaseAdapter;

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

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

        String[] forecastArray = {
                "Today - Sunny - 88/63",
                "Tomorrow - Foggy - 74/58",
                "Sunday - Rainy - 65/59",
                "Monday - Cloudy - 72/67"
        };
        final List<String> weekForecast = new ArrayList<>(Arrays.asList(forecastArray));

        mForecaseAdapter = new ArrayAdapter<>(
                getActivity(),
                R.layout.list_item_forecast,
                R.id.list_item_forecast_textview,
                weekForecast);

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        final ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
        listView.setAdapter(mForecaseAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String forecast = mForecaseAdapter.getItem(position);
                Intent detailActivityIntent = new Intent(getActivity(), DetailActivity.class)
                        .putExtra(Intent.EXTRA_TEXT, forecast);
                startActivity(detailActivityIntent);
            }
        });

        return rootView;
    }
}

fragment_detail.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".DetailActivityFragment">

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

which is nearly identical to

fragment_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivityFragment">

<ListView
    android:id="@+id/listview_forecast"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center" />

</FrameLayout>
6
  • can you post fragment_detail.xml? Commented May 20, 2015 at 15:40
  • Updated the OP with fragment_detail.xml and fragment_main.xml @Blackbelt Commented May 20, 2015 at 15:43
  • @Blackbelt Nope--just the one DetailActivityFragment.java Commented May 20, 2015 at 15:47
  • @Blackbelt Why would deleting that line fix it? I'm not saying it won't, but I don't understand how that line is in the fragment_main.xml and it still works fine. Commented May 20, 2015 at 15:49
  • can you try cleaning up the project and run it again ? Commented May 20, 2015 at 15:52

1 Answer 1

3

You should be using FragmentActivity instead of Activity

public class DetailActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail); //failing here
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I already tried that, and it still didn't work. Furthermore, the Android course is actually using ActionBarActivity instead of Activity or FragmentActivity.
ok so you must use the support library! developer.android.com/tools/support-library/features.html post the imports that are you using.
That fixed it! How come Android Studio doesn't autopopulate that when generating a new Activity with fragment? Also, why didn't I have to extend FragmentActivity in the MainActivity? (MainActivity extends Activity)
hehe, if you choose to use fragments you have to change to FragmentActivity manually.
but why didn't I have to do it for the MainActivity?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.