Android Studio升级至Giraffe后按钮触发的Dialog无法显示求助
Android Studio升级Giraffe后按钮触发的Dialog不显示(菜单触发正常)
我的应用通过Dialog向用户提示信息并获取输入,之前所有Dialog都正常工作。但将Android Studio从Chipmunk升级到Giraffe版本后,按钮触发的Dialog不再显示,但菜单调用的Dialog可以正常弹出(比如点击菜单Quit选项时的退出确认Dialog)。
正常工作的菜单Dialog实现
public void showAlertQuit(String title, String message) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(message); builder.setTitle(title); builder.setCancelable(false); builder.setPositiveButton("Yes", (dialog, which) -> { dialog.dismiss(); finish(); }); builder.setNegativeButton("No", (dialog, which) -> dialog.dismiss()); AlertDialog alertDialog = builder.create(); alertDialog.show(); }
异常场景:Connect按钮触发Dialog无响应
点击Connect按钮时,相关Dialog无法弹出,核心代码如下:
Connect按钮点击事件
public void onClickConnect(View view) { // For running on the emulator which does not support bluetooth if (inEmulator) { address = "00:11:22:33:44:55"; conntype = "bluetooth"; setUiLaps(true); deviceConnected = true; TString = textView.getText().toString(); TString = TString + "\nConnection opened to " + address + ".\nSet laps > 0."; textView.setText(TString); lapsHead.setText(R.string.set_laps); // get the track associated with the address of the timer processTimerAddress(); startVol = Integer.parseInt(Objects.requireNonNull(trackLaneData.get(0).get("startVol"))); falseVol = Integer.parseInt(Objects.requireNonNull(trackLaneData.get(0).get("falseVol"))); winVol = Integer.parseInt(Objects.requireNonNull(trackLaneData.get(0).get("winVol"))); oMenu.setGroupVisible(0, false); // hide the options menu } else { // running on actual hardware /* * Try to connect to a BlueTooth Device that will be sending lap signals * Launch the BTDeviceListActivity to see the available devices and/or do a scan */ Intent serverIntent = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { serverIntent = new Intent(MainActivity.this, BTDeviceListActivity.class); startActivityForResult(serverIntent, Constants.REQUEST_CONNECT_DEVICE); } } }
触发Dialog的核心逻辑
public void processTimerAddress() { // look for a timer and the active track DBHandler db = new DBHandler(MainActivity.this); trackId = db.GetActiveTrackByConnectAddr(conntype, address); if (trackId == -1) { // timer not found, timer and track must be added showTimerAlert("Timer Absent", "Timer with connection: " + conntype + " - " + address + ", does not exist.\n" + "Lets add the Timer and Track"); } else if (trackId == 0) { // timer exists, but there is no active track int timerid = db.GetTimerByConnect(conntype, address); showNoTrackAlert( "Active Track Missing", "Timer with connection: " + conntype + " - " + address + ", is present.\n" + "However, there is no active track. Lets add a track for this timer", timerid); } else { // a track exists for this race // get the track and lane information for this track trackLaneData = db.GetTrackByTrackId(trackId); laneCnt = Integer.parseInt(Objects.requireNonNull(trackLaneData.get(0).get("lanecnt"))); ArrayList<HashMap<String, String>> laneList = db.GetLanesByTrackId(trackId); int s = laneList.size(); for (int i = 0; i < s; i++) { trackLaneData.add(laneList.get(i)); } } db.close(); }
无法显示的Dialog实现
public void showTimerAlert(String title, String message) { Log.d(TAG, "Show Timer Alert " + title); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(message); builder.setTitle(title); builder.setCancelable(false); builder.setPositiveButton("OK", (dialog, which) -> startAddTimerTrack()); builder.setNegativeButton("CANCEL", (dialog, which) -> dialog.dismiss()); AlertDialog alertDialog = builder.create(); alertDialog.show(); }
已尝试的排查手段
- 在菜单中调用
showTimerAlert,Dialog可正常显示; - 在
onClickConnect方法的if语句前、processTimerAddress调用前添加showAlertQuit调用,预期点击Connect按钮弹出退出确认Dialog,但未成功; - 更换Dialog构建时的Context:
- 原代码:
AlertDialog.Builder builder = new AlertDialog.Builder(this); - 改为:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); - 改用全局Context对象:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
以上尝试均未解决问题,恳请帮忙排查。
- 原代码:
内容的提问来源于stack exchange,提问作者Chet
相关产品推荐
相关产品推荐

