所以从调试和注释掉的东西我得出的结论是这个程序的问题出在 addTextView() 方法中。我完全不知道问题出在哪里。我的猜测是在我试图添加到 TextView 的布局中。非常感谢您的帮助。
public class Books extends Activity{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.books);
addTextView("Hello World");
}
public void addTextView(String text)
{
ScrollView viewport = (ScrollView)findViewById(R.id.books);
TextView textview= (TextView)new TextView(this);
textview.setText(text);
//textview.setLayoutParams(new ScrollView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
viewport.addView(textview, new ScrollView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
public void addButton(String text)
{
ScrollView scrollview = (ScrollView)findViewById(R.id.books);
Button btnTag = (Button)new Button(Books.this);
btnTag.setLayoutParams(new ScrollView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText(text);
scrollview.addView(btnTag);
}
}