Android:复合组件上的 InflateException

发布于 2024-11-25 02:17:08 字数 2500 浏览 0 评论 0原文

我正在创建一个非常复杂的复合组件,它会抛出 InflateException 且原因未知。我能够在下面的简化版本中复制该错误,该版本只是一个具有两个文本视图的组件。任何有助于识别错误或追踪错误的帮助将不胜感激。

composite_component.xml

<?xml version="1.0" encoding="utf-8"?>
<com.cc.CompositeComponent 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView
        android:text="One"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:text="Two"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</com.cc.CompositeComponent>

CompositeComponent.java

package com.cc;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

public class CompositeComponent extends LinearLayout {

    public CompositeComponent(Context context) {
    super(context);
    }

    public CompositeComponent(Context context, AttributeSet attributes){
        super(context, attributes);
    }

    protected void onFinishInflate() {
        super.onFinishInflate();
        ((Activity)getContext()).getLayoutInflater().inflate(R.layout.composite_component, this);
    }
}

CompositeActivity.java

package com.cc;

import android.app.Activity;
import android.os.Bundle;

public class CompositeActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.composite_component);
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cc"
    android:versionCode="1"
    android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="CompositeComponent">
    <activity android:name="CompositeActivity"
              android:label="CompositeComponent">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

I'm creating a pretty complex composite component which throws an InflateException with an unknown cause. I was able to replicate the error in a simplified version below, which is just a component with two text views. Any help in identifying the error or tracking it down would be appreciated.

composite_component.xml

<?xml version="1.0" encoding="utf-8"?>
<com.cc.CompositeComponent 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView
        android:text="One"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:text="Two"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</com.cc.CompositeComponent>

CompositeComponent.java

package com.cc;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

public class CompositeComponent extends LinearLayout {

    public CompositeComponent(Context context) {
    super(context);
    }

    public CompositeComponent(Context context, AttributeSet attributes){
        super(context, attributes);
    }

    protected void onFinishInflate() {
        super.onFinishInflate();
        ((Activity)getContext()).getLayoutInflater().inflate(R.layout.composite_component, this);
    }
}

CompositeActivity.java

package com.cc;

import android.app.Activity;
import android.os.Bundle;

public class CompositeActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.composite_component);
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cc"
    android:versionCode="1"
    android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="CompositeComponent">
    <activity android:name="CompositeActivity"
              android:label="CompositeComponent">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-12-02 02:17:08

在您的 composite_component.xml 文件中,尝试将此行:更改

<com.cc.CompositeComponent

为以下内容:

<LinearLayout

然后,创建您的活动传递给 setContentView() 的第二个布局:

File : composite_activity_layout.xml

<com.cc.CompositeComponent
    android:id="@+id/my_composite"
    <!-- other stuff ->>

文件: CompositeActivity.java

protected void onCreate( Bundle state ) {
    super.onCreate( state );

    setContentView(R.layout.composite_activity_layout);
    // ...
}

您所做的事情看起来正在导致递归布局膨胀。您膨胀 CompositeComponent,然后在 onFinishInflate() 方法中,CompositeComponent 膨胀其自身的另一个副本。

另外,您可能想研究 Android 的合并标记,以避免多余的 LinearLayout 闲置。


In your composite_component.xml file, try changing this line:

<com.cc.CompositeComponent

to the following:

<LinearLayout

And then, create a second layout that your activity passes to setContentView():

File: composite_activity_layout.xml

<com.cc.CompositeComponent
    android:id="@+id/my_composite"
    <!-- other stuff ->>

File: CompositeActivity.java

protected void onCreate( Bundle state ) {
    super.onCreate( state );

    setContentView(R.layout.composite_activity_layout);
    // ...
}

What it looks like you are doing is causing a recursive layout inflation. You inflate CompositeComponent, and then in the onFinishInflate() method, CompositeComponent inflates another copy of itself.

Also, you may want to investigate the use of Android's merge tag to avoid having that extra LinearLayout hanging around.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文