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();
其他類:
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
沒有幫助,錯誤: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
這是否工作? – hasan83
這是否工作:)現在我有:公共類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