android.view.InflateException: Binary XML file line #9: Error inflating class <unknown>
Asked Answered
A

4

8

I'm reusing this XML file for multiple ListViews in different activities (which I'm assuming has something to do with it), but I do not see why it would break. The XML file represents the individual items in the ListView.

Here's the XML content:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp" >

    <LinearLayout
        android:id="@+id/post"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="?attr/postBackground"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/postTop"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="8dp" >

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical" >

                    <!-- Title -->

                    <TextView
                        android:id="@+id/title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="sans-serif-light"
                        android:textColor="?attr/postTitle"
                        android:textSize="16sp"
                        android:textStyle="bold" />

                    <!-- subreddit + domain -->

                    <TextView
                        android:id="@+id/subredditDomainVotes"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="sans-serif"
                        android:gravity="left"
                        android:textColor="#828282"
                        android:textSize="13sp" />

                    <TextView
                        android:id="@+id/date"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="sans-serif"
                        android:gravity="left"
                        android:textColor="#828282"
                        android:textSize="13sp" />
                </LinearLayout>

            </LinearLayout>

            <ImageView
                android:id="@+id/imagePreview"
                android:layout_width="fill_parent"
                android:layout_height="200dp"
                android:adjustViewBounds="false"
                android:background="?attr/postPreviewBground"
                android:contentDescription="@string/empty"
                android:scaleType="centerCrop" />

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="?attr/postBottom"
                android:padding="8dp" >

                <!-- Author -->

                <TextView
                    android:id="@+id/author"
                    style="?attr/postButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:textSize="12sp" />

                <!-- Comments -->

                <TextView
                    android:id="@+id/comments"
                    style="?attr/postButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:textSize="12sp" />
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>

</FrameLayout>

And here's the Java code (the error occurs on convertView = (FrameLayout) FrameLayout.inflate(mContext, R.layout.column_post, null);):

@Override
public View getView(int position, View convertView, ViewGroup parent) {

JSONObject thePost = null;
String kind = null;
try {
    thePost = mPosts.getJSONObject(position);
    kind = thePost.getString("kind");
} catch (Exception e) {
    System.out.println("errreoroer");
}

if (null == convertView) {

    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    convertView = new FrameLayout(mContext);

    if (Switches.getInMailView()) {

        convertView = (RelativeLayout) inflater.inflate(
                R.layout.mail_post, parent, false);

        mailHolder = new MailHolder();
        mailHolder.authorView = (TextView) convertView
                .findViewById(R.id.author);
        mailHolder.dateView = (TextView) convertView
                .findViewById(R.id.date);
        mailHolder.subjectView = (TextView) convertView
                .findViewById(R.id.subject);

    } else {

        if (kind.equals("t3")) {
                  //ERROR HAPPENS RIGHT HERE
            convertView = (FrameLayout) FrameLayout.inflate(mContext, R.layout.column_post, null);

            holder = new PostHolder();
            // grab the post view objects
            holder.postTitleView = (TextView) convertView
                    .findViewById(R.id.title);
            holder.dateView = (TextView) convertView
                    .findViewById(R.id.date);
            holder.authorView = (TextView) convertView
                    .findViewById(R.id.author);
            holder.commentsView = (TextView) convertView
                    .findViewById(R.id.comments);
            holder.subredditDomainVotesView = (TextView) convertView
                    .findViewById(R.id.subredditDomainVotes);
            holder.imagePreviewView = (ImageView) convertView
                    .findViewById(R.id.imagePreview);
            holder.postTopView = (LinearLayout) convertView
                    .findViewById(R.id.postTop);

            convertView.setTag(holder);

        }

    }

} else {

    if (Switches.getInMailView())
        mailHolder = (MailHolder) convertView.getTag();
    else {

        if (kind.equals("t3"))
            holder = (PostHolder) convertView.getTag();

    }

}

try {

    if (Switches.getInMailView())
        return buildMailPostItem((RelativeLayout) convertView,
                thePost.getJSONObject("data"), kind);
    else {
        if (kind.equals("t3"))
            return buildGenericPostItem((FrameLayout) convertView,
                    thePost.getJSONObject("data"), kind, position);
    }

} catch (JSONException e) {

    return null;
}

return convertView;

}

Stack trace

11-26 18:23:09.492: E/AndroidRuntime(4616): FATAL EXCEPTION: main
11-26 18:23:09.492: E/AndroidRuntime(4616): Process: com.reditr.app, PID: 4616
11-26 18:23:09.492: E/AndroidRuntime(4616): android.view.InflateException: Binary XML file line #9: Error inflating class <unknown>
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.LayoutInflater.createView(LayoutInflater.java:620)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at com.reditr.adapters.PostAdapter.getView(PostAdapter.java:145)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.AbsListView.obtainView(AbsListView.java:2263)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.ListView.makeAndAddView(ListView.java:1790)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.ListView.fillDown(ListView.java:691)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.ListView.fillFromTop(ListView.java:752)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.ListView.layoutChildren(ListView.java:1630)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.AbsListView.onLayout(AbsListView.java:2091)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.View.layout(View.java:14785)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.View.layout(View.java:14785)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.View.layout(View.java:14785)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.View.layout(View.java:14785)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1585)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.View.layout(View.java:14785)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.View.layout(View.java:14785)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.View.layout(View.java:14785)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.View.layout(View.java:14785)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.View.layout(View.java:14785)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1985)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1742)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.Choreographer.doFrame(Choreographer.java:532)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.os.Handler.handleCallback(Handler.java:733)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.os.Looper.loop(Looper.java:137)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at android.app.ActivityThread.main(ActivityThread.java:4998)
11-26 18:23:09.492: E/AndroidRuntime(4616):     at java.lang.refl
Agata answered 26/11, 2013 at 21:22 Comment(4)
is it full log ? Do you have Caused by statements below ?Dialysis
That's the full log, no caused by statements.Agata
well, first thing : try removing line android:background="?attr/postBackground" and see if you have different log (could crash at another line, or could work).Dialysis
Same error log :( What's strange is that the ListViews in my first activity use this XML file along with the same code with no issues. As soon as a create a new activity and use the same code to generate the ListView(s), that's when the error props up.Agata
A
14

The issue was that I needed to make sure that I was using the same theme across activities.

Agata answered 26/11, 2013 at 22:47 Comment(0)
D
0

I would use the inflator you grabbed from your context rather than the static method from FrameLayout, just to keep the code clear. FrameLayout's inherited inflate() method is just a wrapper around calling Context.getSystemService() then calling inflate. Try changing it to this:

        convertView =
            (FrameLayout)inflater.inflate(mContext,
                                          R.layout.column_post,
                                          parent,
                                          false);

I suspect the crash you are seeing is because you are passing null for the parent and the the default inflate() variant will attempt to attach the new child view you are creating to the parent (which you don't want to do.)

Despumate answered 26/11, 2013 at 22:15 Comment(3)
What is the name of your XML file? I know you said you are re-using it for multiple things, is it the same content just re-named? Is this XML file at res/layout/column_post.xml in your tree?Despumate
Yeah, the xml file is just located in res/layout/column_post.xml. It only crashes when I use it in a separate activity. It works fine if I reuse the same XML file within the same activity.Agata
Just fixed the issue, @Dialysis said to make sure that the activities had the same theme. It turned out they didn't, and boom it worked!Agata
B
0

I had this issue many times in many projects. It was always the same thing that I forgot.

Android Studio → Left menuProjects (Not Android)srcres

drawable and drawable-v24, some of my XML files, however, was located under drawable-v24. I dragged them to drawable, and the problem was gone.

Breana answered 25/6, 2021 at 13:38 Comment(1)
It still isn't clear where one sentence ends and the next one starts. For example, near "my XML files"Pochard
R
-1

If anyone got this exception (android.view.InflateException), search for the cause of the exception if it was because a drawable- resource-is-not-found exception, like the following error message, e.g.,

Caused by: android.content.res.Resources$NotFoundException: Resource "com.example.example:drawable/white_curvy_back"

so it's likely because you're running the application on a device running an Android version earlier than Android 7.0 (Nougat), and you're declaring your resources in the "Drawable-v24" folder which is a version-specific folder introduced in Android 7.0 (API level 24). It is used to store drawable resources that are specifically designed for devices running Android 7.0 or higher. Resources placed in this folder will only be used on devices running Android 7.0 or higher, while devices running earlier versions will ignore these resources, and will throw Resources$NotFoundException when trying to inflate any xml file pointing to any of theses resources.

To fix this issue, if your app does not have any version-specific drawable resources or if you want the resources to be available to all versions of Android, it is safe to move all the resources to the "drawable" folder instead of "drawable-v24". By doing so, the resources will be accessible to all Android versions, including ones earlier than Android 7.

Roid answered 17/10, 2023 at 9:21 Comment(6)
did you use any generative AI at all in the revision you made to this post?Metralgia
@Metralgia Yes, I did. I usually use it to correct my grammatical mistakes and assist in any definitions. I'm curious to know how you became aware of that ? XDRoid
meta.stackoverflow.com/q/424036/11107541Metralgia
@Roid While we don't go into details, suffice it to say that Starball has seen literally thousands of AI-assisted answers here on Stack Overflow, and has become very good at identifying them. While I do hope that one day we can reach the point where tools like ChatGPT can assist with grammar, at the moment we're finding that they often change the meaning of what the user really meant to say. For that reason, the policy also prohibits this use. For now, just rely on your own language and expertise, which appears just fine - Thanks!Concernment
I think it is a good example of ChatGPT (or similar) trying (probably) to do too much, using synonyms where they are probably not needed (or worse, harmful) and partly rewriting sentences.Pochard
You could have achieved most of it just using a good old spell checker (built into all major web browsers (nothing to install)).Pochard

© 2022 - 2024 — McMap. All rights reserved.