继承视图上的setLayoutParams



我的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文件

FATAL EXCEPTION: main过程:fr.cameleoz。nemo, PID: 30304java.lang.StackOverflowError在fr.cameleoz.nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams (InheritedView.java: 32)在fr.cameleoz.nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams (InheritedView.java: 32)在fr.cameleoz.nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams (InheritedView.java: 32)在fr.cameleoz.nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams (InheritedView.java: 32)在fr.cameleoz.nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams (InheritedView.java: 32)在fr.cameleoz.nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams (InheritedView.java: 32)在fr.cameleoz.nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams (InheritedView.java: 32)在fr.cameleoz.nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams (InheritedView.java: 32)在fr.cameleoz.nemo.business.factories.nemoviews.impl.InheritedView.setLayoutParams (InheritedView.java: 32)在[…]

你能告诉我为什么吗?由于

基于堆栈跟踪,您的InheritedView覆盖setLayoutParams(),并且被覆盖的方法调用自己。

每个嵌套的方法调用占用一些堆栈空间,这个无限递归只在堆栈耗尽时终止。

相关内容

  • 没有找到相关文章