Kent Lee
Kent Lee

Reputation: 117

Unable to inflate fragment

I am new to Android Studio and I am trying to build a simple interface with one activity and one fragment. But when I run the app it says "Error inflating class fragment" Here is my code:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
           xmlns:android="http://schemas.android.com/apk/res/android"
           xmlns:app="http://schemas.android.com/apk/res-auto"
           xmlns:tools="http://schemas.android.com/tools"             
           android:layout_width="match_parent"
           android:layout_height="match_parent"              
           android:fitsSystemWindows="true"
           tools:context=".MainActivity">

     <include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>

MainActivity.java:

 package com.example.zhuol.sunshine;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity"
android:orientation="vertical">



<fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:name="com.example.zhuol.sunshine.Fragment_Item"
    android:id="@+id/fragment"
    android:layout_gravity="center_horizontal|bottom" />
</FrameLayout>

fragment_fragment_item.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" tools:context="com.example.zhuol.sunshine.Fragment_Item">

    <!-- TODO: Update blank fragment layout -->
    <TextView android:layout_width="match_parent"                        
    android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

Main Errors:

 11-12 00:47:42.179 10787-10787/com.example.zhuol.sunshine E/AndroidRuntime:         
 FATAL EXCEPTION: main
 11-12 00:47:42.179 10787-10787/com.example.zhuol.sunshine E/AndroidRuntime:           
 Process: com.example.zhuol.sunshine, PID: 10787
 11-12 00:47:42.179 10787-10787/com.example.zhuol.sunshine E/AndroidRuntime:      
 java.lang.RuntimeException: Unable to start activity   
     ComponentInfo{com.example.zhuol.sunshine/com.example.zhuol.sunshine.MainActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class fragment

 E/AndroidRuntime:  Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
  E/AndroidRuntime:  Caused by: java.lang.ClassCastException: com.example.zhuol.sunshine.MainActivity@101ed69b must implement OnFragmentInteractionListener

Upvotes: 0

Views: 84

Answers (1)

random
random

Reputation: 10309

Here's the source of your problem:

com.example.zhuol.sunshine.MainActivity@101ed69b must implement OnFragmentInteractionListener

You should implement OnFragmentInteractionListener in your MainActivity

So if your com.example.zhuol.sunshine.Fragment_Item defines OnFragmentInteractionListener interface with onFragmentInteractionHome, openHome methods

public class Fragment_Item extends Fragment {

    private OnFragmentInteractionListener mListener;

    .
    .


    public interface OnFragmentInteractionListener {
        public void onFragmentInteractionHome(Uri uri);
        public void openHome(View view);
    }

}

Then you should implement OnFragmentInteractionListener in MainActivity and implement onFragmentInteractionHome, openHome methods

public class MainActivity extends Activity implements HomeFragment.OnFragmentInteractionListener {

    .
    .
    .

    @Override
    public void openHome(View view) {
        System.out.println("Success");
    }

    @Override
    public void onFragmentInteractionHome(Uri uri) {
        Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
    }

}

Upvotes: 1

Related Questions