对话框警报的字符串null

时间:2019-04-04 06:39:46

标签: android

我有一个字符串数据,我想使此数据出现在对话框警报中。我已经在对话框警报消息中应用了该消息,但其显示仍然为空。

这是我的问题课

 private String mPembahasan[]= {
            "Konsumsi ayam",
            "Konsumsi sapi"
    };


public  String getCorrectbahas (int a){
        String bahas = mPembahasan[a];
        return bahas;
    }

这是我的测验课

  private String mBahas;

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
     private void gameOver(){
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Quiz.this);
            alertDialogBuilder
                    .setMessage("Game Over! Skor Latihan Anda "    + mScore * 10  +  " Poin")
                    .setMessage(mBahas)
                    .setCancelable(false)
                    .setPositiveButton("Latihan Soal Baru",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    startActivity(new Intent(getApplicationContext(),Quiz.class));

                                }
                            }
                    )
                    .setNegativeButton("Keluar",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    startActivity(new Intent(getApplicationContext(),MainActivity.class));
                                }
                            }
                    );
            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();
        }

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

在对话框中设置空字符串.setMessage(mBahas)

替换此行

.setMessage(mBahas)

收件人

.setMessage(getCorrectbahas(0));

您还需要更改的一件事

 private String mPembahasan[]= {
            "Konsumsi ayam",
            "Konsumsi sapi"
    };

收件人:

private String[] mPembahasan= {"Konsumsi ayam", "Konsumsi sapi"};

答案 1 :(得分:0)

将gameOver函数移出onCreate并像这样初始化变量

private String mBahas;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);
    mBahas=getCorrectbahas(0)
    gameOver();
 }

功能

private void gameOver(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Quiz.this);
        alertDialogBuilder
                .setMessage("Game Over! Skor Latihan Anda "    + mScore * 10  +  " Poin")
                .setMessage(mBahas)
                .setCancelable(false)
                .setPositiveButton("Latihan Soal Baru",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                startActivity(new Intent(getApplicationContext(),Quiz.class));

                            }
                        }
                )
                .setNegativeButton("Keluar",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                startActivity(new Intent(getApplicationContext(),MainActivity.class));
                            }
                        }
                );
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }