Question

I'm getting "Error inflating Class Fragment" error in XML- file when the app starts. I don't understand why?

I've tried replacing the class- file that's causing the error with a similar file, used in another project that doesn't get this error, but i didn't help, so Maybe the issue is not in the class file, but somewhere else?

Error:

01-09 23:16:52.054: E/AndroidRuntime(558): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.exempelfragmentsgrund/com.example.exempelfragmentsgrund.MainActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class fragment

Line XML- file line 15:

<fragment android:name="com.example.exempelfragmentgrund.VisaFragment"
              android:id="@+id/fragment_container_stor"
              android:layout_weight="3"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

VisaFragment.java onCreateView:

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

    if (savedInstanceState != null){
        mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
    }

    return inflater.inflate(R.layout.om_sida, container, false);        
}

Here are the entire files:

error log:

01-09 23:39:24.804: D/dalvikvm(595): GC_FOR_ALLOC freed 46K, 3% free 8843K/9027K, paused 38ms
01-09 23:39:24.815: I/dalvikvm-heap(595): Grow heap (frag case) to 9.232MB for 513744-byte allocation
01-09 23:39:24.884: D/dalvikvm(595): GC_FOR_ALLOC freed <1K, 3% free 9344K/9543K, paused 43ms
01-09 23:39:24.974: D/dalvikvm(595): GC_CONCURRENT freed <1K, 3% free 9344K/9543K, paused 3ms+3ms
01-09 23:39:25.014: D/AndroidRuntime(595): Shutting down VM
01-09 23:39:25.014: W/dalvikvm(595): threadid=1: thread exiting with uncaught exception (group=0x40014760)
01-09 23:39:25.034: E/AndroidRuntime(595): FATAL EXCEPTION: main
01-09 23:39:25.034: E/AndroidRuntime(595): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.exempelfragmentsgrund/com.example.exempelfragmentsgrund.MainActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class fragment
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.app.ActivityThread.access$1500(ActivityThread.java:122)
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.os.Looper.loop(Looper.java:132)
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.app.ActivityThread.main(ActivityThread.java:4025)
01-09 23:39:25.034: E/AndroidRuntime(595):  at java.lang.reflect.Method.invokeNative(Native Method)
01-09 23:39:25.034: E/AndroidRuntime(595):  at java.lang.reflect.Method.invoke(Method.java:491)
01-09 23:39:25.034: E/AndroidRuntime(595):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-09 23:39:25.034: E/AndroidRuntime(595):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-09 23:39:25.034: E/AndroidRuntime(595):  at dalvik.system.NativeStart.main(Native Method)
01-09 23:39:25.034: E/AndroidRuntime(595): Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class fragment
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688)
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
01-09 23:39:25.034: E/AndroidRuntime(595):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:223)
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.app.Activity.setContentView(Activity.java:1780)
01-09 23:39:25.034: E/AndroidRuntime(595):  at com.example.exempelfragmentsgrund.MainActivity.onCreate(MainActivity.java:13)
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
01-09 23:39:25.034: E/AndroidRuntime(595):  ... 11 more
01-09 23:39:25.034: E/AndroidRuntime(595): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.exempelfragmentgrund.VisaFragment: make sure class name exists, is public, and has an empty constructor that is public
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.app.Fragment.instantiate(Fragment.java:500)
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.app.Fragment.instantiate(Fragment.java:468)
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.app.Activity.onCreateView(Activity.java:4132)
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664)
01-09 23:39:25.034: E/AndroidRuntime(595):  ... 20 more
01-09 23:39:25.034: E/AndroidRuntime(595): Caused by: java.lang.ClassNotFoundException: com.example.exempelfragmentgrund.VisaFragment in loader dalvik.system.PathClassLoader[/data/app/com.example.exempelfragmentsgrund-1.apk]
01-09 23:39:25.034: E/AndroidRuntime(595):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:251)
01-09 23:39:25.034: E/AndroidRuntime(595):  at java.lang.ClassLoader.loadClass(ClassLoader.java:540)
01-09 23:39:25.034: E/AndroidRuntime(595):  at java.lang.ClassLoader.loadClass(ClassLoader.java:500)
01-09 23:39:25.034: E/AndroidRuntime(595):  at android.app.Fragment.instantiate(Fragment.java:490)
01-09 23:39:25.034: E/AndroidRuntime(595):  ... 23 more

(layout-large/) activity_main.XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:baselineAligned="false"
    android:orientation="horizontal"    
    android:background="@color/menyBGColor"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment android:name="com.example.exempelfragmentsgrund.MenyFragment"
              android:id="@+id/meny_fragment"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

    <fragment android:name="com.example.exempelfragmentgrund.VisaFragment"
              android:id="@+id/fragment_container_stor"
              android:layout_weight="3"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

</LinearLayout>

VisaFragment.java:

    package com.example.exempelfragmentsgrund;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class VisaFragment extends Fragment {

    static final String ARG_POSITION = "position";
    int mCurrentPosition = -1;

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

        if (savedInstanceState != null){
            mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
        }

        return inflater.inflate(R.layout.om_sida, container, false);        
    }

    @Override
    public void onStart(){
        super.onStart();
System.out.println("VisaFragment onStart");     

        Bundle argument = getArguments();
        if (argument != null){
            visaFragmentArgument(argument.getInt(ARG_POSITION));
        }else if (mCurrentPosition != -1){
            visaFragmentArgument(mCurrentPosition);
        }
    }

    public void visaFragmentArgument(int position) {
System.out.println("VisaFragment VisaFragmentArgument");


                 TextView infoText = (TextView) getActivity().findViewById(R.id.fragmentTextView);
        infoText.setText(getResources().getStringArray(R.array.adressLista)[position]);
                //ArrayLista.infoText[position]);
        TextView infoTitel = (TextView) getActivity().findViewById(R.id.fragmentTextViewTitel);
        infoTitel.setText(getResources().getStringArray(R.array.infoTextTitel)[position]);                  
        }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putInt(ARG_POSITION, mCurrentPosition);
    }
}

om_sida.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:padding="@dimen/infoTextPadding"
    android:background="@color/visaSidaBGColor" >

       <TextView
        android:id="@+id/fragmentTextViewTitel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:layout_marginTop="16dp"
        android:textSize="36sp" />

    <TextView
        android:id="@+id/fragmentTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/fragmentTextViewTitel"
        android:layout_marginTop="20dp"
        android:textSize="18sp" />

</RelativeLayout>

MainActivity.java

    package com.example.exempelfragmentsgrund;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

public class MainActivity extends FragmentActivity implements MenyFragment.onMenyKnappClickListener{

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

        setContentView(R.layout.activity_main);

        if (findViewById(R.id.fragment_container_liten) != null){
            if (savedInstanceState != null){
                return;
            }

            MenyFragment menyFragment = new MenyFragment();
            menyFragment.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction()
            .add(R.id.fragment_container, menyFragment).commit();
        }
    }

    public void onMenyKnappVald (int position){


    VisaFragment visaFrag = (VisaFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_container_stor);

    if (visaFrag != null) {
        visaFrag.visaFragmentArgument(position);

    }else{
        VisaFragment nyttFragment = new VisaFragment();
        Bundle argument = new Bundle ();

        argument.putInt(VisaFragment.ARG_POSITION, position);
        nyttFragment.setArguments(argument);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, nyttFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }


    }

}
Was it helpful?

Solution

Change com.example.exempelfragmentgrund.VisaFragment to com.example.exempelfragmentsgrund.VisaFragment you are missing a 's'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top