Android:复合组件上的 InflateException
我正在创建一个非常复杂的复合组件,它会抛出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的
composite_component.xml
文件中,尝试将此行:更改为以下内容:
然后,创建您的活动传递给
setContentView()
的第二个布局:File :
composite_activity_layout.xml
文件:
CompositeActivity.java
您所做的事情看起来正在导致递归布局膨胀。您膨胀
CompositeComponent
,然后在onFinishInflate()
方法中,CompositeComponent
膨胀其自身的另一个副本。另外,您可能想研究 Android 的合并标记,以避免多余的
LinearLayout
闲置。In your
composite_component.xml
file, try changing this line:to the following:
And then, create a second layout that your activity passes to
setContentView()
:File:
composite_activity_layout.xml
File:
CompositeActivity.java
What it looks like you are doing is causing a recursive layout inflation. You inflate
CompositeComponent
, and then in theonFinishInflate()
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.