从 MainActivity 启动 AlertDialog 片段
Starting AlertDialog fragment from MainActivity
我相信对此有一个简单的答案。我正在尝试从没有按钮的主 activity 启动 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();
}
}
创建一个 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");
}
我相信对此有一个简单的答案。我正在尝试从没有按钮的主 activity 启动 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();
}
}
创建一个 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");
}