user3479468
user3479468

Reputation: 45

Problems Inflating Fragment on Android

I'm trying to load a fragment in my Main Activity which will become one of my navigation tabs later. the problem is that the app crashes and says that the fragment cannot be inflated. I found other similar questions but none of the solutions could help me

Here's my code

MainActivity.java

public class MainActivity extends FragmentActivity {


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

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.projecttabs.MainActivity">

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.projecttabs.FragmentAbout"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp" />

</LinearLayout>

FrgagmentAbout.java

public class FragmentAbout extends Fragment {

    public View OnCreateView(LayoutInflater l, ViewGroup v, Bundle savedInstanceState){

        return l.inflate(R.layout.fragment_about,v,false);
    }

}

fragment_about.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#000">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="182dp"
        android:text="ButtonTest" />

</RelativeLayout>

logCat

FATAL EXCEPTION: main
Process: com.example.projecttabs, PID: 1709
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projecttabs/com.example.projecttabs.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
    at android.app.ActivityThread.access$700(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4998)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
    at android.app.Activity.setContentView(Activity.java:1928)
    at com.example.projecttabs.MainActivity.onCreate(MainActivity.java:25)
    at android.app.Activity.performCreate(Activity.java:5243)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
    ... 11 more
Caused by: java.lang.IllegalStateException: Fragment com.example.projecttabs.FragmentAbout did not create a view.
    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:314)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)

For what I understand, MainActivity class sets the contentView to main_activity layout, then, in main_activity.xml file, FragmentAbout class is called to replace the fragment by fragment_about layout. am I right?

please, help me!

thanks!

Upvotes: 1

Views: 506

Answers (1)

Gil Vegliach
Gil Vegliach

Reputation: 3572

It should be onCreateView() with a lowercase 'o' in FragmentAbout

In the future, try using the java annotation @Override to avoid this stupid bugs

Upvotes: 1

Related Questions