0

原因不明の InflateException をスローするかなり複雑な複合コンポーネントを作成しています。以下の単純化されたバージョンでエラーを再現できました。これは、2 つのテキスト ビューを持つ単なるコンポーネントです。エラーを特定したり、追跡したりするための助けをいただければ幸いです。

複合コンポーネント.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>

4

1 に答える 1

0

composite_component.xmlファイルで、次の行を変更してみてください。

<com.cc.CompositeComponent

以下に:

<LinearLayout

次に、アクティビティが渡す 2 つ目のレイアウトを作成しますsetContentView()

ファイル: 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ます。

于 2011-07-20T20:45:30.880 に答える