你好,我试图显示一个TextView
的字符串。我动态地这样做,这是代码:
LinearLayout layout=(LinearLayout) this.findViewById(R.id.Layout);
EditText editText=(EditText) this.findViewById(R.id.textView);
Button button=(Button) this.findViewById(R.id.btnAdd);
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText(display);
this.layout.addView(tv);
,但它在this.layout.addView(tv);
上遇到错误。你能告诉我我做错了什么吗?
提前多多感谢!
更新(完整活动代码):
public class CalendarDate extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar_date);
Bundle extras = getIntent().getExtras();
String dday = extras.getString("currentday");
String dmonth =extras.getString("currentmonth");
String dyear = extras.getString("currentyear");
ActionBar ab = getActionBar();
ab.setTitle(dday+"/"+dmonth+"/"+dyear );
Bundle extraz =getIntent().getExtras();
String display = extras.getString("EXTRA_MESSAGE");
setContentView(R.layout.activity_calendar_date);
LinearLayout layout=(LinearLayout) this.findViewById(R.id.Layout);
EditText editText=(EditText) this.findViewById(R.id.textView);
Button button=(Button) this.findViewById(R.id.btnAdd);
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText(display);
layout.addView(tv);
}
答案 0 :(得分:3)
删除this
,仅删除layout.addView(tv);
。使用this
仅用于引用实例变量。 (任何方法之外的类中的变量)