2013-09-05 106 views
-1

你好,我正在嘗試顯示一個字符串與TextView。我動態這樣做的,這是代碼:Android:顯示帶有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

什麼是錯誤? this.layout.addView(tv)中的 – Manu

+0

;說佈局不能解決或不是一個領域。 – SoCo

+0

嘗試:layout.addView(tv); – Tobiel

回答

3

刪除this,只是layout.addView(tv);。僅使用this來引用實例變量。 (任何方法以外的類中的變量)

+0

op問了一個類似的問題http://stackoverflow.com/questions/18642713/android-textview-makes-app-stop。我不認爲消除這個問題會有所作爲 – Raghunandan

相關問題