简体   繁体   中英

how to add rating bar in Linear Layout Dynamically in Android?

I have tried to add rating bar in Linear Layout at dynamically after some textfields. But I'm getting NullPointerException at the line = lL.addView(rating); Can someone help me please how to do this.Thanks in Advance.

Here is my xml file.

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

In order to add it to the LinearLayout from your xml you need to get a "reference" to this LinearLayout in your code. For that you need to ad id it in xml and get an element using this id in your code.

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

Also use more explanatory names. "lL" will cause you headache in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM