开发者

Android: InflateException on composite component

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>



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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜