我試圖在一些文本域後動態地在線性佈局中添加評級欄。但我得到NullPointerException在行= lL.addView(評級);有人可以幫我請怎麼做這個。感謝提前。如何在Android中動態添加Linear Layout中的評分欄?
這是我的xml文件。
<ScrollView
android:id="@+id/scrollField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btnDELETE"
android:layout_below="@+id/textTitle"
android:layout_marginTop="10dp" >
<LinearLayout
android:id="@+id/linearLayoutRatingDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_EmployeeName"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:text="dfkyjrtl"
android:textColor="#2E1F1F" />
<TextView
android:id="@+id/tv_TaskName"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="6dp"
android:text="dfkyjrtl"
android:textColor="#2E1F1F" />
<TextView
android:id="@+id/tv_TaskDate"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="6dp"
android:text="dfkyjrtl"
android:textColor="#2E1F1F" />
<TextView
android:id="@+id/tv_TaskRate"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="6dp"
android:text="dfkyjrtl"
android:textColor="#2E1F1F" />
</LinearLayout>
</ScrollView>
Hare is my Activity code.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.task_details);
Intent intent = getIntent();
str_IntentName = intent.getExtras().getString("EMP_NAME");
str_IntentTaskName = intent.getExtras().getString("TASK_NAME");
str_IntentDate = intent.getExtras().getString("TASK_DATE");
str_IntentRate = intent.getExtras().getString("TASK_RATE");
numStar = Integer.valueOf(str_IntentRate);
Log.e("integer numStar "," = " + numStar);
lL = (LinearLayout)findViewById(R.id.linearLayoutRatingDetails);
tvEmpName.setText(str_IntentName);
tvTaskName.setText(str_IntentTaskName);
tvDate.setText(str_IntentDate);
tvRate.setText(str_IntentRate);
Create_RatingBar();
}
private void Create_RatingBar()
{
stepSize = (float) 0.5;
rating = new RatingBar(Task_Details.this);
rating.setNumStars(numStar);
rating.setStepSize(stepSize);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
param.topMargin = 500;
rating.setLayoutParams(param);
lL.addView(rating);
}
我會提示'lL'爲null – LordRaydenMK
嘗試發佈您的活動整個代碼而不是片段。 –
你在哪裏初始化lL,把完整的代碼放在這裏,因爲LordRaydenMK說100%IL是空的! – Techfist