我想以编程方式在其中创建带有文本视图的滚动视图。我的代码如下所示。它有效,但是当我在 onSizeChanged 中执行 getLayoutParams 时,我得到 InvocationTargetException。我的代码有什么问题?
public MyActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new MyLayout(this));
}
protected class MyLayout extends RelativeLayout
{
TextView tv;
public MyLayout(Context context)
{
super(context);
ScrollView sv = ScrollView(context);
tv = new TextView(context);
tv.setText("Hello World");
sv.addView(tv);
addView(sv);
}
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld)
{
LayoutParams params = (LayoutParams)tv.getLayoutParams(); // I have InvocationTargetException here. What's wrong?
}
}
}