标签:stack-overflow android-inflate android-custom-view 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 {
@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
解决方法:
在您的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.
标签:stack-overflow,android-inflate,android-custom-view,android 来源: https://codeday.me/bug/20191101/1982818.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。