AlertDialog not displaying

时间:2015-07-28 16:12:47

标签: android alertdialog android-appcompat

I have an alert dialog that is not displaying. I've read through many other posts here on this issue, and followed all of the suggested advice, yet still not seeing anything.

I am checking if there is an available network connection, and if not, displaying an AlertDialog to prompt the user to connect to WIFI.

In my logging, it correctly shows that no connection is available, and that it is about to show the AlertDialog, but then nothing is visible.

public class PopulateDataActivity extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    populateData();
}

void populateData() {
    boolean hasConnection = Utility.isNetworkAvailable(this);
    if(!hasConnection){
        Log.d(LOG_TAG, "No connection.");
        showConnectDialog(this);
    }
    Log.d(LOG_TAG, "Trying to populate languages");
    //get data from server
}
}

abstract class BaseActivity extends AppCompatActivity {

public void showConnectDialog(Context context)
{
    Log.d(LOG_TAG, "Showing connection dialog.");
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setMessage(R.string.connect_to_internet);
    builder.setCancelable(false);
    builder.setPositiveButton(R.string.connect_yes_button, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
        }
    });
    builder.setNegativeButton(R.string.connect_no_button, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            finish();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}
}

Log output:

D/PopulateDataActivity No connection.
D/BaseActivity﹕ Showing connection dialog.
I/AppCompatDelegate﹕ The Activity's LayoutInflater already has a Factory installed so we can not install AppCompat's
D/PopulateDataActivity Trying to populate languages

Is that AppCompat the issue here, or am I missing something else?

Because the dialog is not displayed, the activity then just continues on and tries to make the data connection, which then throws an exception.

I have now found that when it continues on in the execution, a later AlertDialog is shown when the data retrieval is unsuccessful. This displays fine, and I can see that the first one (the ConnectDialog referenced here) has been displayed beneath it, but I never can actually reach it.

This issue seems to be related: The Activity's LayoutInflater already has a Factory installed so we can not install AppCompat's

1 个答案:

答案 0 :(得分:0)

使用AppCompat v7:22.1.0时遇到了同样的问题。我创建了一个小应用程序来尝试隔离问题,直到我应用与我的主应用程序相同的样式。当我设置自己的主题以使用'alertDialogTheme'属性提醒对话框时,culprid就是这样。在这个特定的活动中,我不需要更改默认的样式。所以我创建了一个新主题,将'alertDialogTheme'属性设置回默认值并将主题应用于活动。

<item name="alertDialogTheme">@style/Theme.AppCompat.Light.Dialog.Alert</item>

这不是一个完整的解决方案,但我希望它可以帮助某人完成它。

注意:我的警告对话框没有按钮。即使使用自定义主题,添加按钮也会使问题消失。