2014-07-25 25 views
0

我試圖在一些文本域後動態地在線性佈局中添加評級欄。但我得到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); 
    } 
+0

我會提示'lL'爲null – LordRaydenMK

+0

嘗試發佈您的活動整個代碼而不是片段。 –

+0

你在哪裏初始化lL,把完整的代碼放在這裏,因爲LordRaydenMK說100%IL是空的! – Techfist

回答

0

爲了從您的XML添加到LinearLayout中,你需要在你的代碼來獲得一個「參考」這個LinearLayout中。爲此,您需要在xml中使用ID它,並在您的代碼中使用此代碼獲取使用此ID的元素。

public void onCreate(Bundle savedInstanceState){ 
    //Some other code for other views 
    ViewGroup container = (ViewGroup) findViewById(R.id.linearLayoutRatingDetails); 
    setupRatingBar(container) 
} 

private void setupRatingBar(ViewGroup ratingBarContainer){ 
    RatingBar ratingBar = new RatingBar(Task_Details.this); 
    //All methods you need to initialize the rating bar 
    //including setNumStars(), setStepSize() and layout params 

    ratingBarContainer.addView(ratingBar); 
} 

也使用更多的解釋性名稱。 「lL」會在未來讓你頭痛。

+0

感謝您的回覆,但我沒有得到您。請你能解釋鋤頭做什麼? – jvd

+0

我在代碼中進行了更改,但未顯示評級欄。 – jvd

+0

我編輯了我的答案。 – Zzokk

相關問題