-1

你好,我正在尝试显示一个带有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);
}
4

1 回答 1

3

删除this,刚刚layout.addView(tv);。仅用于this引用实例变量。(任何方法之外的类中的变量)

于 2013-09-05T16:14:32.043 回答