【问题标题】:AlertDialog.Builder wont show in current fragmentAlertDialog.Builder 不会显示在当前片段中
【发布时间】:2018-06-17 10:09:47
【问题描述】:

所以我在 TemplateHome 活动(默认片段是 HomeFragment)中有一个名为 appinfo 的侧面菜单项,它将在 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>

任何建议都有帮助,谢谢

【问题讨论】:

    标签: android android-activity fragment android-alertdialog


    【解决方案1】:

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

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

    【讨论】:

    • 嗯,我不认为方法 switchhomefragment 有问题。让我感到困惑的是 infoaplikasi 方法中的“getLayoutInflater()”,它是导致 alertdialog 仅在 TemplateHome 活动中显示的原因吗?我也已经更新了我的代码:)
    • 您的片段在 Activity 中,因此如果您从 Activity 调用对话框,无论如何用户都可以看到它。在您的布局中,将LinearLayout `android:layout_width="match_parent"` 更改为wrap_content 并将所有fill_parent 更改为match_parentfill_parent 已弃用。您是否尝试在 Activity 的布局上添加一个简单的按钮并从那里调用 infoaplikasi?
    • 嘿,非常感谢您的帮助:),但我已经解决了这个问题。祝您有美好的一天!
    • 没问题 :) 你能支持我的回答吗?我需要 4 个声望点才能编写 cmets ;)
    【解决方案2】:

    抱歉,我刚刚意识到我使用 HomeFragment.class 设置了 fragmentclass 值。这就是为什么警报一直出现在 HomeFragment 的原因

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      • 2016-09-17
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多