2
我正在試着用RatingBar做AlertDialog,並且出現一個我不知道如何解決的錯誤。這裏是我的代碼:AlertDialog上RatingBar的問題
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>
的問題是,「老鼠倉」和「buttpost」的findViewById是空的,我不知道另一種方式將偵聽器添加到該評級欄和按鈕。有任何想法嗎?任何幫助,將不勝感激。非常感謝!
它的工作!謝謝 – lluisu