【问题标题】:AlertDialog wont work with custom layoutAlertDialog 不适用于自定义布局
【发布时间】: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


【解决方案1】:

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

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

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

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

对想要从AlertDialog访问的其他视图执行相同操作

【讨论】:

  • 正是我自己看到的......我问的很傻......但它不计算评级你能帮我吗?
  • @KostasMatrix:我一定会尝试请解释更多关于下一期的信息
猜你喜欢
  • 2013-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-01
  • 1970-01-01
相关资源
最近更新 更多