2013-10-27 4 views
1

에서 텍스트 뷰를 설정 :안드로이드 내가 코드가 다른 클래스

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(); 

다른 클래스 : 내가 Main 클래스에이 코드를 넣어

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); 
      } 



    } 

작동한다 :

이 순간
 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 TextView text = new TextView (primaryFieldsView.getContext()); ... 괜찮 았습니까? – Johny0987

관련 문제