I'm sure there is a simple answer for this. I'm trying to start an AlertDialogFragment (RegForXapo) from my main activity without a button. It pops up but app force closes when i click the positive or negative button.
Here is my main
sharedPref = getSharedPreferences(mypref, Context.MODE_PRIVATE);
if (sharedPref.getBoolean("firstRun", true)) {
//start AlertDialog
FragmentManager fm = getSupportFragmentManager();
RegForXapo reg = new RegForXapo();
fm.show(reg, "dialog");
}
Here is my 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();
}
}