Marcus Kuffner
Marcus Kuffner

Reputation: 1

android.view.InflateException on setContentView layout containing fragment

While making my app I realized that I used a fragment class instead of an activity, but instead of restarting I made a new XML layout file and put it in a fragment component instead. Now when I call setContentView on that layout my app crashes, is there a way I can use this fragment class still or do I need to restart with an Activity class?

Here is the XML file:

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

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.redacted.redacted.ui.pSelect.pSelect"
        tools:layout="@layout/p_select"
    />

 </LinearLayout>

Any help at all is appreciated.

p_select:

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

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Selection Page"
    android:layout_margin="10dp"
    android:textColor="#FFFFFF"
    android:textSize="22dp"
    android:textStyle="bold"
    android:id="@+id/title"/>

<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/title"
    android:id="@+id/select_RV"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_below="@+id/select_RV">

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="id 1"
    android:onClick="button1"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="id 2"
    android:onClick="button2"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="id 3"
    android:onClick="button3"/>

</LinearLayout>


</RelativeLayout>

Here is the error message:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.redacted.pnews, PID: 10863 java.lang.IllegalStateException: Could not execute method for android:onClick at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390) at android.view.View.performClick(View.java:7125) at android.view.View.performClickInternal(View.java:7102) at android.view.View.access$3500(View.java:801) at android.view.View$PerformClick.run(View.java:27336) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) at android.view.View.performClick(View.java:7125)  at android.view.View.performClickInternal(View.java:7102)  at android.view.View.access$3500(View.java:801)  at android.view.View$PerformClick.run(View.java:27336)  at android.os.Handler.handleCallback(Handler.java:883)  at android.os.Handler.dispatchMessage(Handler.java:100)  at android.os.Looper.loop(Looper.java:214)  at android.app.ActivityThread.main(ActivityThread.java:7356)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)  Caused by: android.view.InflateException: Binary XML file line #8 in com.redacted.pnews:layout/p_fragment: Binary XML file line #8 in com.redacted.pnews:layout/p_fragment: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #8 in com.redacted.pnews:layout/p_fragment: Error inflating class fragment Caused by: java.lang.IllegalArgumentException: Binary XML file line #8: Must specify unique android:id, android:tag, or have a parent with an id for com.redacted.pnews.ui.pSelect.pSelect at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3717) at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:120) at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:405) at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:387) at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1069) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:997) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961) at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084) at android.view.LayoutInflater.inflate(LayoutInflater.java:682) at android.view.LayoutInflater.inflate(LayoutInflater.java:534) at android.view.LayoutInflater.inflate(LayoutInflater.java:481) at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at com.redacted.pnews.MainActivity.backBTN(MainActivity.java:91) at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) at android.view.View.performClick(View.java:7125) at android.view.View.performClickInternal(View.java:7102) at android.view.View.access$3500(View.java:801) at android.view.View$PerformClick.run(View.java:27336) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

I would rather not post the class code for personal reasons

The onCreateView is called within an onclick listener in the MainActivity class, and the primary use of the fragment class is to fill in the recyclerview in p_select, so I dont the fragment class code affects this error.

Upvotes: 0

Views: 965

Answers (2)

Marcus Kuffner
Marcus Kuffner

Reputation: 1

Turns out it was an error within the class I made, thanks for the help but it seems the answer is unrelated to the question.

Upvotes: 0

ianhanniballake
ianhanniballake

Reputation: 200020

The error message says

Error inflating class fragment Caused by: java.lang.IllegalArgumentException: Binary XML file line #8: Must specify unique android:id, android:tag, or have a parent with an id for com.redacted.pnews.ui.pSelect.pSelect

So add an android:id to your <fragment>:

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

  <fragment
    android:id="@+id/select_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.redacted.redacted.ui.pSelect.pSelect"
    tools:layout="@layout/p_select"
  />

</LinearLayout>

Upvotes: 1

Related Questions