AlertDialog.Builder 不会在当前片段中显示
AlertDialog.Builder wont show in current fragment
所以我在 TemplateHome activity 中有一个名为 appinfo 的侧边菜单项(默认片段是 HomeFragment),它将在 AlertDialog.Builder
中显示应用程序版本
我能够很好地显示警报对话框,但每次我点击应用信息菜单时,应用程序总是会转到 HomeFragment/TemplateHome activity
现在有人知道如何在当前片段中显示警报对话框吗?
这是我的菜单代码
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();
}
这是我调用 switchhomefragment 代码的地方
@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
,也许这里有问题。
PS。在您的代码中使用专有名称约定。 java 的代码约定可以在这里找到:http://www.oracle.com/technetwork/java/codeconventions-135099.html
谢天谢地,我刚刚意识到我用 HomeFragment.class 设置了 fragmentclass 值。这就是为什么警报一直转到 HomeFragment 的原因
所以我在 TemplateHome activity 中有一个名为 appinfo 的侧边菜单项(默认片段是 HomeFragment),它将在 AlertDialog.Builder
中显示应用程序版本我能够很好地显示警报对话框,但每次我点击应用信息菜单时,应用程序总是会转到 HomeFragment/TemplateHome activity
现在有人知道如何在当前片段中显示警报对话框吗?
这是我的菜单代码
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();
}
这是我调用 switchhomefragment 代码的地方
@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
,也许这里有问题。
PS。在您的代码中使用专有名称约定。 java 的代码约定可以在这里找到:http://www.oracle.com/technetwork/java/codeconventions-135099.html
谢天谢地,我刚刚意识到我用 HomeFragment.class 设置了 fragmentclass 值。这就是为什么警报一直转到 HomeFragment 的原因