AlertDialog.Builder 不会显示在当前片段中



所以我在TemplateHome活动中有一个名为appinfo的侧菜单项(默认片段是HomeFragment(,它将在AlertDialog.Builder中显示应用程序版本

我能够很好地显示警报对话框,但是每次我单击应用程序信息菜单时,该应用程序将始终转到主页片段/模板主页活动

现在有谁知道如何在当前片段中显示警报对话框?

这是我的菜单代码

public void switchhomefragment(MenuItem item) {
fragment = null;
Class fragmentClass = HomeFragment.class;
switch (item.getItemId()) {
case R.id.sidemenu_profil:
fragmentClass = ProfileFragment.class;
currentfragment = "home";
break;
case R.id.sidemenu_pencarian:
fragmentClass = HomeFragment.class;
currentfragment = "home";
break;
case R.id.sidemenu_pendaftaran:
fragmentClass = PendaftaranFragment.class;
currentfragment = "home";
break;
case R.id.sidemenu_fav:
break;
case R.id.sidemenu_tentang:
fragmentClass = TentangFragment.class;
currentfragment = "home";
break;
case R.id.sidemenu_info:
infoaplikasi();
break;
case R.id.action_home:
fragmentClass = HomeFragment.class;
currentfragment = "home";
break;
case R.id.sidemenu_signout:
editor.remove("sessioniduser");
editor.commit();
methodUmum.tologin(this);
break;
default:
fragmentClass = HomeFragment.class;
currentfragment = "home";
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
Log.d("templatehome", "isi error : " + e);
e.printStackTrace();
}
fragmentManager.beginTransaction().addToBackStack(null).replace(R.id.flContent, fragment, currentfragment).commit();
item.setChecked(true);
mDrawer.closeDrawers();
}

这就是我调用 switchhome片段代码的地方

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switchhomefragment(item);
return true;
}

这是 AlertDialog.Builder 代码

public void infoaplikasi() {
dialog = new AlertDialog.Builder(TemplateHome.this);
inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.info_aplikasi, null);
dialog.setView(dialogView);
dialog.setCancelable(true);
dialog.setNegativeButton("TUTUP", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
dialog.show();
}

这是我info_aplikasi XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="@dimen/ukr16"
android:paddingRight="@dimen/ukr16"
android:paddingTop="@dimen/ukr16">
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="@dimen/ukr8"
android:text="App Version"
android:textAllCaps="true"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="version 2.1"
android:textSize="16sp" />
</LinearLayout>

任何建议都是有帮助的,谢谢

在我的应用程序中,会弹出您的对话框,因此infoaplikasi方法中的代码是可以的。我认为这个问题switchhomefragment方法。你叫得对吗?在该方法中infoaplikasi();上放置断点,并查看它是否已执行。另外,向我们展示R.layout.info_aplikasi,也许这是一个问题。

在代码中使用专有名称约定。java的代码约定可以在这里找到:http://www.oracle.com/technetwork/java/codeconventions-135099.html

很糟糕,我刚刚意识到我用 HomeFragment.class 设置了片段类值。 这就是为什么警报一直转到 HomeFragment 的原因

最新更新