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

-2

I have a class fragment ... in which I am inflating an xml file. This fragment is being called in MainActivity ... but when I run 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 code for the class fragment:

    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:

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);
            }
        }
    });

}
    
asked by anonymous 04.10.2016 / 20:45

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);
}
    
04.10.2016 / 20:50
0

1st choice:

Check the layout_frag_1.xml , if the parent tag has the fragment id, eg

<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>

2nd option:

Check the imports, they should be with supports, considering 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;

3rd option:

In the top menu of your AndroidStudio, click Build and then Clean Project     

04.10.2016 / 21:06