添加Splash Screen后setPositiveButton文本不显示问题排查
问题:添加启动页后AlertDialog确认按钮文本不显示,点击空白区域功能正常
为应用添加启动页(Splash Screen)后,AlertDialog中setPositiveButton设置的“Finish”文本消失,对应位置仅显示空白,但点击该空白区域仍能正常执行finishQuiz()方法。
启动页代码(SplashScreen.java)
package com.example.numbers; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import androidx.appcompat.app.AppCompatActivity; public class SplashScreen extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); new Handler().postDelayed(new Runnable() { @Override public void run() { startActivity(new Intent(SplashScreen.this, MainActivity.class)); finish(); } }, 7000); } }
主页面代码(MainActivity.java)
package com.example.numbers; import android.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ TextView questionTextView; TextView totalQuestionTextView; Button ansA,ansB,ansC,ansD; Button btn_submit; int score=0; int totalQuestion = QuestionAnswer.question.length; int currentQuestionIndex =0; String selectedAnswer=""; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); totalQuestionTextView = findViewById(R.id.total_question); questionTextView = findViewById(R.id.question); ansA = findViewById(R.id.ans_a); ansB = findViewById(R.id.ans_b); ansC= findViewById(R.id.ans_c); ansD = findViewById(R.id.ans_d); btn_submit = findViewById(R.id.btn_submit); ansA.setOnClickListener(this); ansB.setOnClickListener(this); ansC.setOnClickListener(this); ansD.setOnClickListener(this); btn_submit.setOnClickListener(this); totalQuestionTextView.setText("You have "+totalQuestion +" questions to answer"); loadNewQuestion(); } private void loadNewQuestion(){ if(currentQuestionIndex == totalQuestion){ finishQuiz(); return; } questionTextView.setText(QuestionAnswer.question[currentQuestionIndex]); ansA.setText(QuestionAnswer.choices[currentQuestionIndex][0]); ansB.setText(QuestionAnswer.choices[currentQuestionIndex][1]); ansC.setText(QuestionAnswer.choices[currentQuestionIndex][2]); ansD.setText(QuestionAnswer.choices[currentQuestionIndex][3]); selectedAnswer=""; } private void finishQuiz(){ String passStatus; if(score >= totalQuestion*0.6){ passStatus = "You passed - well done!"; }else{ passStatus = "You failed this time - but keep trying!"; } new AlertDialog.Builder(this) .setTitle(passStatus) .setMessage("You scored "+score+" out of "+totalQuestion) .setCancelable(false) .setPositiveButton("Finish", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { moveTaskToBack(true); android.os.Process.killProcess(android.os.Process.myPid()); System.exit(1); } }) .show(); } private void restartQuiz(){ score = 0; currentQuestionIndex=0; loadNewQuestion(); } @Override public void onClick(View view){ ansA.setBackgroundColor(Color.WHITE); ansB.setBackgroundColor(Color.WHITE); ansC.setBackgroundColor(Color.WHITE); ansD.setBackgroundColor(Color.WHITE); Button clickedButton = (Button) view; if(clickedButton.getId() == R.id.btn_submit) { if(!selectedAnswer.isEmpty()){ if(selectedAnswer.equals(QuestionAnswer.correctAnswers[currentQuestionIndex])){ score++; }else{ clickedButton.setBackgroundColor(Color.MAGENTA); } currentQuestionIndex++; loadNewQuestion(); }else{ } } else{ selectedAnswer=clickedButton.getText().toString(); clickedButton.setBackgroundColor(Color.YELLOW); } } private class OnClickListener implements DialogInterface.OnClickListener { @Override public void onClick(DialogInterface dialogInterface, int i) { } } }
问题截图

问题排查与解决
核心原因
MainActivity里自定义了一个空实现的内部类OnClickListener,它实现了DialogInterface.OnClickListener,但这个类名和Activity本身实现的View.OnClickListener重名,导致系统渲染AlertDialog按钮时出现逻辑冲突,进而使按钮文本无法正常显示。启动页代码本身没有问题,只是启动流程的类加载顺序变化触发了这个隐藏的命名冲突。
解决方法
直接删除MainActivity底部的这个无用内部类即可:
private class OnClickListener implements DialogInterface.OnClickListener { @Override public void onClick(DialogInterface dialogInterface, int i) { } }
删除后重新编译运行,AlertDialog的“Finish”按钮文本就能正常显示了。
内容的提问来源于stack exchange,提问作者JesusGirl2000
相关产品推荐
相关产品推荐

