我的Android应用程序出现问题。我想通过这样做来创建一个新的视图:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
LinearLayout l = (LinearLayout)findViewById(R.id.mainActivity);
f = new InheritedView(this);
f.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
l.addView(f);
}
使用
public class InheritedView extends ImageView implements AbstractView
public InheritedView(Context context) {
super(context);
}
但是当我测试时,我的应用程序在执行setLayoutParams(..)时崩溃了java.lang.StackOverflowError。 但是,以下工作:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
LinearLayout l = (LinearLayout)findViewById(R.id.mainActivity);
f = new ImageView(this);
f.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
l.addView(f);
}
LOGCAT文件
致命的例外:主要 处理:fr.cameleoz.nemo,PID:30304 java.lang.StackOverflowError的 at fr.cameleoz.nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams(InheritedView.java:32) at fr.cameleoz.nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams(InheritedView.java:32) at fr.cameleoz.nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams(InheritedView.java:32) at fr.cameleoz.nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams(InheritedView.java:32) at fr.cameleoz.nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams(InheritedView.java:32) at fr.cameleoz.nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams(InheritedView.java:32) at fr.cameleoz.nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams(InheritedView.java:32) at fr.cameleoz.nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams(InheritedView.java:32) at fr.cameleoz.nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams(InheritedView.java:32) 在[...]
你能解释一下为什么吗? 感谢答案 0 :(得分:0)
根据堆栈跟踪,您的InheritedView
会覆盖setLayoutParams()
,并且重写的方法会自行调用。
每个嵌套方法调用占用一些堆栈空间,并且只有在堆栈用完时才会终止此无限递归。