【发布时间】:2015-09-19 22:24:48
【问题描述】:
我正在尝试使用评级和 cmets 构建用户体验对话框。
我已经建立了对话框,一切正常,但是当我按下肯定按钮时,由于某种原因它崩溃了......
我的onCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_profile);
rateButton = (FloatingActionButton) findViewById(R.id.rate);
shareButton = (FloatingActionButton) findViewById(R.id.share);
ratingBar = (RatingBar) findViewById(R.id.ratingBar);
ratingComment = (EditText) findViewById(R.id.ratingComment);
ratingNum = (TextView) findViewById(R.id.ratingNum);
ratingCommentText = (TextView) findViewById(R.id.Comment);
setupButtonEvents();
}
setupButtonEvents()
private void setupButtonEvents() {
rateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog.Builder builder = new AlertDialog.Builder(UserProfile.this);
View newLayout = getLayoutInflater().inflate(R.layout.rate_dialog,(ViewGroup)findViewById(R.id.rootLayout));
builder.setView(newLayout)
.setTitle(getString(R.string.rateDialogTitle))
.setPositiveButton(R.string.posButtonRate, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
insertDataToDatabaseFromRating();
}
}).create();
builder.show();
}
});
还有insertDataToDatabaseFromRating()
private void insertDataToDatabaseFromRating() {
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
percent = (int) ratingBar.getRating();
ratingNum.setText("" + percent);
}
});
comTxt = ratingComment.getText().toString();
if (!comTxt.isEmpty()) {
ratingCommentText.setText(comTxt);
}
}
Logcat out Error
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RatingBar.setOnRatingBarChangeListener(android.widget.RatingBar$OnRatingBarChangeListener)' on a null object reference
at com.order.app.order.UserProfile.insertDataToDatabaseFromRating(UserProfile.java:144)
第 144 行:ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
谁能帮我解决这个问题??? 提前谢谢!!!!
【问题讨论】:
-
我建议扩展
DialogFragment。这是一种比AlertDialog.builder.更好、更面向对象的方式
标签: android android-layout android-alertdialog ratingbar