AlertDialog 不适用于自定义布局

AlertDialog wont work with custom layout

我正在尝试构建一个包含评分和评论的用户体验对话框。

我已经构建了对话框并且一切正常,但是当我按下肯定按钮时它由于某种原因崩溃了...

我的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() {

谁能帮我解决这个问题??? 提前致谢!!!

可能 RatingBarAlertDialog 布局内。要从 AlertDialog 布局访问视图,请使用在 builder.setView(newLayout).

中传递的视图对象

在您的情况下,将 View 参数添加到 insertDataToDatabaseFromRating 方法,然后访问 RatingBar 作为:

private void insertDataToDatabaseFromRating(View view) {
 ratingBar = (RatingBar)view. findViewById(R.id.ratingBar);
 .....
}

并在调用时将 newLayout 对象传递给 insertDataToDatabaseFromRating

对其他想要从 AlertDialog

访问的视图执行相同操作