Error - Binary XML file line #30: Binary XML file line #30: Error inflating class Fragment

Asked

Viewed 761 times

-1

I have an Fragment...class in which I am inflating an xml file. This Fragment is being called in Mainactivity...but when executed this error appears

java.lang.Runtimeexception: Unable to start Activity Componentinfo{com.br.comunicacao/com.br.comunicacao.Mainactivity}: android.view.Inflateexception: Binary XML file line #30: Binary XML file line #30: Error inflating class Fragment at android.app.Activitythread.performLaunchActivity(Activitythread.java:2434) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:2494) at android.app.Activitythread.access$900(Activitythread.java:153) at android.app.Activitythread$H.handleMessage(Activitythread.java:1347) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.Activitythread.main(Activitythread.java:5451) at java.lang.reflect.Method.invoke(Native Method) at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:726) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:616) Caused by: android.view.Inflateexception: Binary XML file line #30: Binary XML file line #30: Error inflating class Fragment at android.view.Layoutinflater.inflate(Layoutinflater.java:539) at android.view.Layoutinflater.inflate(Layoutinflater.java:423) at android.view.Layoutinflater.inflate(Layoutinflater.java:374) at com.android.Internal.policy.Phonewindow.setContentView(Phonewindow.java:393) at android.app.Activity.setContentView(Activity.java:2215) at com.br.comunicacao.MainActivity.onCreate(Mainactivity.java:27) at android.app.Activity.performCreate(Activity.java:6323) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.Activitythread.performLaunchActivity(Activitythread.java:2387) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:2494)  at android.app.Activitythread.access$900(Activitythread.java:153)  at android.app.Activitythread$H.handleMessage(Activitythread.java:1347)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.Activitythread.main(Activitythread.java:5451)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:726)  at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:616)

This is my Fragment class code:

    public class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup conteiner, Bundle saveInstanceState){
        View view = inflater.inflate(R.layout.layout_frag_1, conteiner);

        return null;
    }


}

And this is Mainactivity’s:

public class MainActivity extends FragmentActivity {
FragmentManager fm = getSupportFragmentManager();

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

    String[] lista = new String[]{"ALMOÇO","BEBIDAS","COMO ESTOU ME SENTINDO","FAMILIA"};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,lista);
    ListView lv = (ListView) findViewById(R.id.listView2);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> lv, View view, int position, long id) {
            if (position == 1){
                Fragment1 fragment1 = (Fragment1) fm.findFragmentById(R.id.fragment1);
            }
        }
    });

}

2 answers

1


You are returning null in view creation.

Try:

public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup conteiner, Bundle saveInstanceState){
    return inflater.inflate(R.layout.layout_frag_1, conteiner, false);
}
  • I already tried this, but the same error occurs.

  • Tried with false, as third parameter of the inflate method?

  • I just saw here the null return when I returned the view worked. Thank you!

0

1st option:

Check the layout_frag_1.xml, if the parent tag has Fragment id, example:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/fragment1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</LinearLayout>

second option:

Check the Imports, should be with Supports, given that you are using the getSupportFragmentManager(), examples:

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;

third option:

From the top menu of your Androidstudio, click Build and then in Clean Project

Browser other questions tagged

You are not signed in. Login or sign up in order to post.