【问题标题】:Starting AlertDialog fragment from MainActivity从 MainActivity 启动 AlertDialog 片段
【发布时间】:2015-05-19 21:16:22
【问题描述】:

我确信对此有一个简单的答案。我正在尝试从没有按钮的主要活动中启动 AlertDialogFragment (RegForXapo)。当我点击正面或负面按钮时,它会弹出,但应用程序强制关闭。

这是我的主要内容

sharedPref = getSharedPreferences(mypref, Context.MODE_PRIVATE);
    if (sharedPref.getBoolean("firstRun", true)) {
        //start AlertDialog
        FragmentManager fm = getSupportFragmentManager();
        RegForXapo reg = new RegForXapo();
fm.show(reg, "dialog");
    }

这是我的对话

import android.os.Bundle;
import android.app.DialogFragment;
import android.app.Dialog;
import android.app.*;
import android.content.DialogInterface;
import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
import android.content.Context;
import android.net.Uri;


 import android.content.*;public class RegForXapo extends DialogFragment
 {
    private Context context;

public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage(R.string.xapoask);
    builder.setPositiveButton(R.string.positivebutton, 
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            //dismiss dialog and set to never appear. take user to xapo reg
            SharedPreferences sharedPref = context.getSharedPreferences("MySharedPrefs",0);
            Editor editor = sharedPref.edit();
            editor.putBoolean("firstRun",false);
            Intent launchBrowser = new Intent(Intent.ACTION_VIEW, Uri.parse(URL));
            startActivity(launchBrowser);

        }
    });
    builder.setNegativeButton(R.string.negativebutton, 
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            //dismiss dialog and set to never appear
            SharedPreferences sharedPref = context.getSharedPreferences("MySharedPrefs",0);
            Editor editor = sharedPref.edit();
            editor.putBoolean("firstRun",false);

        }
    });
    builder.setNeutralButton(R.string.neutralbutton,
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            //dismiss dialog and set to reappear
            // no code necesary
        }


    });


    return builder.create();
}

 }

【问题讨论】:

    标签: android android-fragments fragment android-alertdialog android-fragmentactivity


    【解决方案1】:

    创建一个 RegForXapo (DialogFragment) 对象,然后show它。

    sharedPref = getSharedPreferences(mypref, Context.MODE_PRIVATE);
    if (sharedPref.getBoolean("firstRun", true)) {
        //start AlertDialog
        FragmentManager fm = getSupportFragmentManager();
        RegForXapo regForXapo = new RegForXapo();
        regForXapo.show(fm, "dialog");
    }
    

    【讨论】:

    • 现在出现错误,没有适用于(android.support.v4.app.FragmentManager,期间)的方法..
    • 试试getFragmentManager() 而不是getSupportFragmentManager()
    • 将 FragmentManager fm 更改为 android.app.FragmentManager fm... 现在可以使用了。但是当我点击负号或正号按钮时,我会强制关闭。
    • 已下载猫日志。这是我的对话框类中的 sharedPrefs 的问题。你能帮忙还是我应该创建一个新问题,因为我的原始问题在技术上得到了回答。
    • 我建议创建一个新问题!然后你可以在这里发布链接并标记我,我会检查出来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多