java - AlertDialog 上 RatingBar 的问题

标签 java android android-alertdialog ratingbar

我正在尝试使用 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 为空,我不知道另一种方法可以将监听器添加到该评级栏和按钮。有任何想法吗?任何帮助,将不胜感激。非常感谢!

最佳答案

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

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

Button buttpost = (Button) dialoglayout.findViewById(R.id.dialogfacebook);

关于java - AlertDialog 上 RatingBar 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14294850/

相关文章:

android - MXPlayer 之类的音量控制

android - 从 Android AlertDialog 复制文本?

android - 当我使用变量 String 时更改 alertDialog 标题的颜色

java - 为什么 FPS 不打印在我的窗口中?

Java 3DES 实现(学术)

java - Lombok 各种构造函数的注释?

java - FFT 实现

Android RelativeLayout间距

android - 如何有一个圆形的 TextView

Android-如何等待 AlertDialog 检查?