从非活动中启动Dialog

时间:2014-11-12 22:39:05

标签: java android android-alertdialog android-dialogfragment android-context

我有这个对话框,直到我添加代码尝试启动另一个Dialog然后它崩溃与DialogFragment.class错误。 popup.show是发生错误的地方。

公共类ProcessLocation扩展了DialogFragment {

Context context;
SharedPreferences domainToName;
SharedPreferences nameToDomain;
SharedPreferences defaults;
PopupMessage popup;
int locationTally;

// Constructor
ProcessLocation(Context context) {
    this.context = context;
}


 public void processLocation(String domain, String name, String password) {

        domainToName = context.getSharedPreferences("domainToName",  Context.MODE_PRIVATE); //domain name = friendly name
        nameToDomain = context.getSharedPreferences("nameToDomain",  Context.MODE_PRIVATE); //friendly name = domain name/password/acm
        defaults = context.getSharedPreferences("Defaults",  Context.MODE_PRIVATE); //friendly name = domain name/password/acm
        String checkName = nameToDomain.getString(name,null);

        locationTally = defaults.getInt("tally",0);
        final Editor domainEdit = domainToName.edit();
        final Editor nameEdit = nameToDomain.edit();
        //popup = new PopupMessage();

        // Check to see if the location friendly name exists already, if so the entry is overwritten with new data
        if (checkName == null) {
             locationTally++;
             updatePreferences(locationTally, name, domain, password);
        //   popup.message = "Location has been added";
        //   popup.show(getFragmentManager(), "PopupMsgFragment");
            }
        else {
            nameEdit.remove(name);
            nameEdit.commit();
            domainEdit.remove(domain);
            domainEdit.remove(domain+"Pw");
            domainEdit.commit();
            updatePreferences(locationTally, name, domain, password);
        }

    }

 public void updatePreferences(int tally, String name, String domain, String password) {

     popup = new PopupMessage();

     domainToName = context.getSharedPreferences("domainToName",  Context.MODE_PRIVATE);    //domain name = friendly name
     nameToDomain = context.getSharedPreferences("nameToDomain",  Context.MODE_PRIVATE); //friendly name = domain name/password/acm
     defaults = context.getSharedPreferences("Defaults",  Context.MODE_PRIVATE); //friendly name = domain name/password/acm
     final Editor domainEdit = domainToName.edit();
     final Editor nameEdit = nameToDomain.edit();
     final Editor defaultEdit = defaults.edit();

     String defaultLocation = defaults.getString("defaultLocation", "None Set");
     if(defaultLocation.equals("")) {
         defaultEdit.putString("defaultLocation",name);
     }

     nameEdit.putString(name,domain);
     nameEdit.commit();
     defaultEdit.putInt("tally",tally);
     defaultEdit.commit();
     domainEdit.putString(domain,name);
     domainEdit.putString(domain+"Pw",password);
     domainEdit.commit();

     System.out.println("mycOutput:- Tally: " + locationTally);
     System.out.println("mycOutput:- Domain from Name: " + nameToDomain.getString(name,""));
     System.out.println("mycOutput:- Name from Domain: " + domainToName.getString(domain,""));
     System.out.println("mycOutput:- Password: " + domainToName.getString(domain+"Pw",""));

     popup.popupType = "info";
     popup.message = name + " has been added successfully";
     popup.show(getActivity().getFragmentManager(), "PopupMsgFragment");

 }

}

包含Popup的类是:

public class PopupMessage extends DialogFragment {


String message = "";
AddLocation addLocation;
String popupType = "";

Context mContext;

public PopupMessage() {
    mContext = getActivity();
}
// Constructor


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    addLocation = new AddLocation();

    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    if (popupType.equals("addloc")) {
        builder.setMessage(message)
           .setPositiveButton("Add Location", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   if (popupType.equals("addloc")) {
                   addLocation.show(getFragmentManager(), "PopupMsgFragment");
                   }
               }
           })
           .setNegativeButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
          //
            };
           });
    }
    if (popupType.equals("info")) {
        builder.setMessage(message)
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {

            }
        }); 
    }

    // Create the AlertDialog object and return it
    return builder.create();
}

}

由于

ERROR MEESSAGE(全部抛出NullPointer):

enter image description here

2 个答案:

答案 0 :(得分:0)

您没有提供AddLocation类。假设它与ProcessLocation不同。

在PopupMessage类中,尝试添加:

Context mContext;

public PopupMessage() {
    mContext = getActivity();
}

并在ProcessLocation类中,修改如下:

替换popup.show(getFragmentManager(), "PopupMsgFragment");

popup.show(getActivity().getFragmentManager(), "PopupMsgFragment");

答案 1 :(得分:0)

<强> ProcessLocation

 final Editor nameEdit = nameToDomain.edit();
            popup = new PopupMessage(this);

            // Check to see if the location friendly name exists already, if so the entry is overwritten with new data
            if (checkName == null) {
                 nameEdit.putString(name,domain);

<强>弹出

public class PopupMessage extends DialogFragment {
    Context myContext;
    String message = "";
    AddLocation addLocation;

    public PopupMessage(Context context){
        this.myContext = context;
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        addLocation = new AddLocation();

        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(this.myContext);
        builder.setMessage(message)
               .setPositiveButton("Add Location", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       addLocation.show(getFragmentManager(), "PopupMsgFragment");
                   }
               })
               .setNegativeButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
              //
            };
        });
        // Create the AlertDialog object and return it
        return builder.create();
    } }