尝试膨胀自定义视图时遇到stackoverflow错误

尝试膨胀自定义视图时遇到stackoverflow错误,第1张

概述我有custom_layout.xml:<?xmlversion="1.0"encoding="utf-8"?><com.example.MyCustomLayoutxmlns:android="http://schemas.android.com/apkes/android"android:layout_width="match_parent"android:

我有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 {@OverrIDeprotected 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

解决方法:

在您的custom_layout.xml中,将< 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类中设置方向).现在,当AndroID加载my_activity.xml时,它将找到您的自定义视图并将其实例化.实例化您的自定义视图时,AndroID会在MyCustomLayout构造函数中为custom_layout xml文件充气.发生这种情况时,它将再次找到< com.example.MyCustomLayout ...(来自刚刚膨胀的custom_layout.xml),这将导致MyCustomLayout再次实例化.这是一个递归调用,它将最终引发StackOverflowError.

总结

以上是内存溢出为你收集整理的尝试膨胀自定义视图时遇到stackoverflow错误全部内容,希望文章能够帮你解决尝试膨胀自定义视图时遇到stackoverflow错误所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1085312.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存