Levenlol
Levenlol

Reputation: 415

Adding a fragment causes crash

I'm stuck with a fragment-related problem. I have a splash screen that will "load" for 3 secs and then will create a new Intent and start the MainActivity.

The main activity code:

public class MainActivity extends AppCompatActivity {

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

and the corresponding XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   tools:context="com.example.paolo.ebolatrack.MainActivity">


    <fragment
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:name="com.example.paolo.ebolatrack.NewsFragment"
       ></fragment>

</RelativeLayout>

and the xml for the fragment:

<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.paolo.ebolatrack.NewsFragment">

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

</FrameLayout>

and here's the error I'm getting (full trace here):

10-26 12:05:05.153 2855-2855/com.example.paolo.ebolatrack E/AndroidRuntime: FATAL EXCEPTION: main
10-26 12:05:05.153 2855-2855/com.example.paolo.ebolatrack E/AndroidRuntime: Process: com.example.paolo.ebolatrack, PID: 2855
10-26 12:05:05.153 2855-2855/com.example.paolo.ebolatrack E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.paolo.ebolatrack/com.example.paolo.ebolatrack.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
...

Adding the fragment seems to crash the app. I'm using Android Studio.

Upvotes: 1

Views: 3146

Answers (1)

Konrad Krakowiak
Konrad Krakowiak

Reputation: 12365

Here you have answer :

Caused by: java.lang.IllegalArgumentException: Binary XML file line #11: Must specify unique android:id, android:tag, or have a parent with an id for com.example.paolo.ebolatrack.NewsFragment

Add id for your fragment in layout xml:

<fragment
   android:id="@+id/some_frag"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:name="com.example.paolo.ebolatrack.NewsFragment"
   ></fragment>

this is missing line :

 android:id="@+id/some_frag"

Upvotes: 1

Related Questions