0

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

我能够很好地显示警报对话框,但是每次我单击 appinfo 菜单时,该应用程序将始终转到 HomeFragment/TemplateHome 活动

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

这是我的菜单代码

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>

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

4

2 回答 2

1

在我的应用程序中,您的对话框会弹出,因此infoaplikasi方法内的代码是可以的。我认为问题在于switchhomefragment方法。你正确地称呼它吗?infoaplikasi();在该方法中放置一个断点 并查看它是否被执行。另外,告诉我们R.layout.info_aplikasi,也许这是一个问题。

PS。在您的代码中使用正确的名称约定。可以在此处找到 Java 的代码约定:http ://www.oracle.com/technetwork/java/codeconventions-135099.html

于 2018-06-17T10:21:26.420 回答
0

不好意思,我刚刚意识到我用 HomeFragment.class 设置了片段类的值。这就是为什么警报一直出现在 HomeFragment 上的原因

于 2018-06-17T11:32:15.913 回答