【问题标题】:Getting stackoverflow error while trying to inflate custom view尝试膨胀自定义视图时出现 stackoverflow 错误
【发布时间】:2012-05-14 17:21:40
【问题描述】:

我有 custom_layout.xml:

<?xml version="1.0" encoding="utf-8"?>

<com.example.MyCustomLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

<!-- different views -->

</com.example.MyCustomLayout>

及其类:

public class MyCustomLayout extends LinearLayout {

public MyCustomLayout(Context context, AttributeSet attrs) {
    super(context, attrs);

    LayoutInflater.from(context).inflate(R.layout.custom_layout, this, true);
    setUpViews();
    }
//different methods
}

还有活动,其中包括这个布局:

public class MyActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_activity);

    setUpViews();
}

还有 my_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

    <com.example.MyCustomLayout
        android:id="@+id/section1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
    <com.example.MyCustomLayout
        android:id="@+id/section2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />

</LinearLayout>

所以,当我从 LayoutInflater.from(context).inflate(R.layout.custom_layout, this, true); 删除评论块并以图形模式转到 my_activity.xml 时,我遇到了问题。 Eclipse 思考然后崩溃。看起来它试图多次夸大我的自定义视图,但我不明白为什么。当我重新启动 Eclipse 时,我在错误日志中收到此错误:java.lang.StackOverflowError

【问题讨论】:

    标签: android stack-overflow android-custom-view android-inflate


    【解决方案1】:

    在您的custom_layout.xml 中,将&lt;com.example.MyCustomLayout 替换为另一个布局(例如LinearLayout):

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
    <!-- different views -->
    
    </LinearLayout>
    

    甚至更好地使用merge 标签(并在MyCustomLayout 类中设置orientation)。现在当Android 加载my_activity.xml 时,它会找到您的自定义View 并实例化它。当您的自定义视图将被实例化时,Android 将在MyCustomLayout 构造函数中膨胀custom_layout xml 文件。发生这种情况时,它将再次找到&lt;com.example.MyCustomLayout ...(来自刚刚膨胀的custom_layout.xml),这导致MyCustomLayout 再次被实例化。这是一个递归调用,它最终会抛出StackOverflowError

    【讨论】:

      【解决方案2】:

      这条线的存在

      LayoutInflater.from(context).inflate(R.layout.custom_layout, this, true);
      

      在自定义布局对象的构造函数中导致自依赖调用的无限递归,导致堆栈溢出。

      你没有理由这样做。

      也许你最好的办法是挖掘一个其他人工作的自定义布局类的例子。

      【讨论】:

      • 没有这一行 - 我在尝试 findViewById 时得到了 null
      • 也许,但这不是解决问题的正确方法。我建议您找到某人的自定义布局的工作示例,作为应该如何完成的机制的指南。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      • 2012-05-16
      • 1970-01-01
      • 2015-11-05
      相关资源
      最近更新 更多