简体   繁体   English

没有错误,但没有显示警告对话框

[英]No errors but alert dialog not displaying

I have non-activity class MenuHandler to handle menu-related events, I am trying to display developers message in alert dialog box, this message fetch from firebase real time database. 我有非活动类MenuHandler来处理与菜单相关的事件,我试图在alert对话框中显示开发人员消息,此消息从firebase实时数据库中获取。

Everything is fine but alert dialog not displaying, I try debugger to check there any error in database but I got value properly from database. 一切都很好,但警报对话框没有显示,我尝试调试器检查数据库中是否有任何错误,但我从数据库正确得到了价值。 No Error to get value from database. 从数据库获取值没有错误。

when I select menu from MainActivity , developerMessage toast shows then nothing happened. 当我从MainActivity选择菜单时, developerMessage toast显示没有任何反应。

I pass context to MenuHandler class properly. 我正确地将context传递给MenuHandler类。 Can I Show Alerdialog in using context? 我可以在使用上下文中显示Alerdialog吗? or I need to write code only in MainActivity only(ie only activity class). 或者我只需要在MainActivity编写代码(即只有活动类)。

Code: 码:

 public void developersMessage()
    {

        if (isInternetOn()) {

            Toast.makeText(mContext,"Loading message please wait",Toast.LENGTH_SHORT).show();

            DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();

            mDatabase.child("version_1_0").child("dev").addListenerForSingleValueEvent(new ValueEventListener() {

                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                        try {

                            final AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.AppCompatAlertDialogStyle);
                            builder.setTitle(mContext.getString(R.string.welcome_msg));
                            builder.setMessage(dataSnapshot.child("dev_msg").getValue(String.class));
                            builder.setPositiveButton("ok",null);

                            builder.create().show();
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }


                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        }
        else
        {
            Toast.makeText(mContext,"please turn on internet ",Toast.LENGTH_SHORT).show();
        }
    }

Build your AlertDialog out of 构建您的AlertDialog

mDatabase.child("version_1_0").child("dev").addListenerForSingleValueEvent(...);

The addListenerForSingleValueEvent(...) method runs on a different background thread, so the UI can't be updated inside off UI thread. addListenerForSingleValueEvent(...)方法在不同的后台线程上运行,因此无法在UI线程内部更新UI。 Thus building your AlertDailog out of the addListenerForSingleValueEvent(...) method should solve your problem. 因此,使用addListenerForSingleValueEvent(...)方法构建AlertDailog可以解决您的问题。

You should use activity context to create AlertDialog. 您应该使用活动上下文来创建AlertDialog。

And please debug to make sure onDataChange() is called 并请调试以确保onDataChange()

Also try with runOnUiThread: https://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable) 也可以尝试使用runOnUiThread: https//developer.android.com/reference/android/app/Activity.html#runOnUiThread (java.lang.Runnable

yourActivity.runOnUiThread(new Runnable() { public void run() {... code to create & show dialog ...} })

I got the answer, There nothing wrong in the code but bug lies in how I send context from mainactivity 我得到了答案,代码没有错,但错误在于我如何从mainactivity发送上下文

here I wrongly init Menuhandler class 在这里我错误地初始化了Menuhandler类

public boolean onOptionsItemSelected(MenuItem item) {

 final MenuHandler mMenuHandler = new MenuHandler(this.getApplicationContext());//bug lies here

if we are showing alertdialog then we need this way to pass context so our alertdialog display properly 如果我们显示alertdialog然后我们需要这种方式来传递上下文,以便我们的alertdialog正确显示

public boolean onOptionsItemSelected(MenuItem item) {
        final MenuHandler mMenuHandler = new MenuHandler(MainActivity.this);//this is right way to pass context

hope this is helpful to someone in future. 希望这对将来有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM