2

我正在尝试使用 RatingBar 执行 AlertDialog,但我遇到了一个我不知道如何修复的错误。这是我的代码:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getResources().getString(R.string.messageAskFB))
    .setCancelable(true)
    .setPositiveButton("Post on Facebook", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            try {
                Intent act = new Intent (RutaActualActivity.this, ActualizaEstado.class);
                act.putExtra(getString(R.string.package_name), mens);
                startActivity(act);
                } catch (Throwable e) {
                    e.printStackTrace();
                }

            }
    });

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.layout_dialog, (ViewGroup) getCurrentFocus());
final int id_poiii=p.getId_poi();

builder.setView(dialoglayout);

AlertDialog lala = builder.create();

RatingBar rat = (RatingBar) lala.findViewById(R.id.dialogRatingBar);
rat.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { 

    public void onRatingChanged(RatingBar ratingBar, float rating,  boolean fromUser) { 
        TaskRating rattask = new TaskRating();
        rattask.execute(id_poiii,ratingBar.getNumStars());
    } 
});

Button buttpost = (Button) lala.findViewById(R.id.dialogfacebook);
buttpost.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        Intent act = new Intent (RutaActualActivity.this, ActualizaEstado.class);
        act.putExtra(getString(R.string.package_name), mens);
        startActivity(act);
    }
});


lala.show();

这是 layout_dialog 的 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/dialogfacebook"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/titleAskFB" />

    <TextView
        android:id="@+id/dialogTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/rate_this_point"
        android:textSize="20sp" />

    <RatingBar
        android:id="@+id/dialogRatingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

</LinearLayout>

问题是 'rat' 和 'buttpost' 的 findViewById 为空,而且我不知道另一种将侦听器添加到该评级栏和按钮的方法。有任何想法吗?任何帮助,将不胜感激。非常感谢!

4

1 回答 1

1

尝试从 AlertDialog 布局中找到 Button 和 RatingBar :

RatingBar rat = (RatingBar) dialoglayout.findViewById(R.id.dialogRatingBar);

Button buttpost = (Button) dialoglayout.findViewById(R.id.dialogfacebook);
于 2013-01-12T15:40:09.337 回答