2013-10-27 32 views
1

我有代碼:的Android設置的TextView從其他類

LinearLayout primaryFieldsView = (LinearLayout) findViewById(R.id.mainLayout); 
     for(int j=0; j<5; j++){ 
      TextView text = new TextView(this); 
      text.setText("The Value of i is :"); // <-- does it really compile without the + sign? 
      text.setTextSize(12); 
      text.setGravity(Gravity.LEFT); 
      text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
      primaryFieldsView.addView(text); 
      } 

我想,這段代碼是從另一個類調用,例如: 在主類:

new PrimaryFieldsView().setPrimaryFields(); 

其他類:

public class PrimaryFieldsView extends Activity{ 

    public void setPrimaryFields(){ 

     LinearLayout primaryFieldsView = (LinearLayout) findViewById(R.id.mainLayout); 
     for(int j=0; j<5; j++){ 
      TextView text = new TextView(this); 
      text.setText("The Value of i is :"); // <-- does it really compile without the + sign? 
      text.setTextSize(12); 
      text.setGravity(Gravity.LEFT); 
      text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
      primaryFieldsView.addView(text); 
      } 



    } 

當我把這個代碼放在Main類的工作中:

 LinearLayout primaryFieldsView = (LinearLayout) findViewById(R.id.mainLayout); 
     for(int j=0; j<5; j++){ 
      TextView text = new TextView(this); 
      text.setText("The Value of i is :"); // <-- does it really compile without the + sign? 
      text.setTextSize(12); 
      text.setGravity(Gravity.LEFT); 
      text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
      primaryFieldsView.addView(text); 
      } 

在這一刻,我有錯誤

10-27 21:32:58.389: E/AndroidRuntime(2907): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.passreader/com.example.passreader.views.CouponView}: java.lang.NullPointerException 

回答

0
public void setPrimaryFields(Activity activity) 
    { 
     LinearLayout primaryFieldsView = (LinearLayout) activity.findViewById(R.id.mainLayout); 
     for(int j=0; j<5; j++){ 
      TextView text = new TextView(this); 
      text.setText("The Value of i is :"); // <-- does it really compile without the + sign? 
      text.setTextSize(12); 
      text.setGravity(Gravity.LEFT); 
      text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
      primaryFieldsView.addView(text); 
      } 
    } 

主:

setPrimaryFields(this); 

PrimaryFieldsView類不必雖然延長任何東西。

+0

沒有幫助,錯誤:10-27 22:04:19.759:E/AndroidRuntime(2981):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.passreader/com.example.passreader.views .CouponView}:java.lang.NullPointerException – Johny0987

+0

這是否工作? – hasan83

+0

這是否工作:)現在我有:公共類PrimaryFieldsView { \t \t公共無效setPrimaryFields(活動活動){ \t \t \t \t的LinearLayout primaryFieldsView =(LinearLayout中)activity.findViewById(R.id.mainLayout);對於(int j = 0; j <5; j ++){ \t \t { \t \t TextView text = new TextView(primaryFieldsView.getContext()); ...沒關係? – Johny0987