没有错误,但不显示警告对话框
No errors but alert dialog not displaying
我有非activityclassMenuHandler
来处理菜单相关的事件,
我正在尝试在警告对话框中显示开发人员消息,此消息从 firebase
实时数据库中获取。
一切正常,但未显示警告对话框,我尝试调试器检查数据库中是否存在任何错误,但我从数据库中正确获取了值。从数据库中获取值没有错误。
当我 select 来自 MainActivity
的菜单时,developerMessage
toast 显示然后什么也没有发生。
我把context
传给了MenuHandler
class。
我可以在 using context 中显示 Alerdialog
吗?
或者我只需要在 MainActivity
中编写代码(即仅 activity class)。
代码:
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();
}
}
使用
构建您的 AlertDialog
mDatabase.child("version_1_0").child("dev").addListenerForSingleValueEvent(...);
addListenerForSingleValueEvent(...)
方法在不同的后台线程上运行,因此 UI 无法在 UI 线程内更新。因此,使用 addListenerForSingleValueEvent(...)
方法构建 AlertDailog 应该可以解决您的问题。
您应该使用 activity 上下文来创建 AlertDialog。
并请调试以确保 onDataChange()
被调用
也试试 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 ...} })
我得到了答案,
代码没有错,但错误在于我如何从 mainactivity
发送上下文
这里我错误地初始化了Menuhandler class
public boolean onOptionsItemSelected(MenuItem item) {
final MenuHandler mMenuHandler = new MenuHandler(this.getApplicationContext());//bug lies here
如果我们显示警报对话框,那么我们需要这种方式来传递上下文,以便我们的警报对话框正确显示
public boolean onOptionsItemSelected(MenuItem item) {
final MenuHandler mMenuHandler = new MenuHandler(MainActivity.this);//this is right way to pass context
希望这对以后的人有所帮助。
我有非activityclassMenuHandler
来处理菜单相关的事件,
我正在尝试在警告对话框中显示开发人员消息,此消息从 firebase
实时数据库中获取。
一切正常,但未显示警告对话框,我尝试调试器检查数据库中是否存在任何错误,但我从数据库中正确获取了值。从数据库中获取值没有错误。
当我 select 来自 MainActivity
的菜单时,developerMessage
toast 显示然后什么也没有发生。
我把context
传给了MenuHandler
class。
我可以在 using context 中显示 Alerdialog
吗?
或者我只需要在 MainActivity
中编写代码(即仅 activity class)。
代码:
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();
}
}
使用
构建您的 AlertDialogmDatabase.child("version_1_0").child("dev").addListenerForSingleValueEvent(...);
addListenerForSingleValueEvent(...)
方法在不同的后台线程上运行,因此 UI 无法在 UI 线程内更新。因此,使用 addListenerForSingleValueEvent(...)
方法构建 AlertDailog 应该可以解决您的问题。
您应该使用 activity 上下文来创建 AlertDialog。
并请调试以确保 onDataChange()
被调用
也试试 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 ...} })
我得到了答案, 代码没有错,但错误在于我如何从 mainactivity
发送上下文这里我错误地初始化了Menuhandler class
public boolean onOptionsItemSelected(MenuItem item) {
final MenuHandler mMenuHandler = new MenuHandler(this.getApplicationContext());//bug lies here
如果我们显示警报对话框,那么我们需要这种方式来传递上下文,以便我们的警报对话框正确显示
public boolean onOptionsItemSelected(MenuItem item) {
final MenuHandler mMenuHandler = new MenuHandler(MainActivity.this);//this is right way to pass context
希望这对以后的人有所帮助。