【问题标题】:Android button onClick to display an AlertDialog application crashAndroid 按钮 onClick 显示一个 AlertDialog 应用程序崩溃
【发布时间】:2013-03-12 13:51:36
【问题描述】:

我试图在按下提交按钮时显示一个 AlertDialog。

Button submit = (Button) findViewById(R.id.submitButton);
            submit.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(Application1GoodExample.this);
                    builder.setMessage("Your form has been successfully submitted");
                    TextView textView = (TextView) findViewById(android.R.id.message);
                    textView.setTextSize(40);
                    builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int which) {
                              dialog.cancel();           
                        } });

                    builder.show();

                }

            });

我相信这与它试图访问findViewById(android.R.id.message)有关

我在我的 XML 文件中定义了 TextView,如下所示:

 <TextView android:id="@+id/message" 
        android:layout_width="285dp"
        android:layout_height="78dp" 
        android:textColor="#000000" 
        android:textSize="45sp" 
        android:gravity="center"
        />

我只想更改默认的 AlertDialog 消息“您的表单已成功提交”字体大小,但是当我按下提交按钮时我的程序崩溃了

【问题讨论】:

    标签: android fonts dialog message alert


    【解决方案1】:

    您正在尝试访问 android.R,只需删除“android”,它应该可以工作(您的资源在 com.yourproject.R,而不是 android.R)。

    findViewById(android.R.id.message)
    

    【讨论】:

      【解决方案2】:

      首先,如果要在 XML 中为 TextView 设置文本大小,则 MainActivity 中的 setContentView() 中的布局必须包含对它的引用。 其次,您应该将 android.R.id.message 替换为 R.id.message。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多