簡體   English   中英

從非活動啟動對話框

[英]Launching Dialog from non-activity

我有一個有效的對話框,直到添加代碼以嘗試啟動另一個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();
}

}

謝謝

錯誤消息(均拋出NullPointer):

在此處輸入圖片說明

您未提供AddLocation類。 假設它與ProcessLocation是不同的類。

在PopupMessage類中,嘗試添加:

Context mContext;

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

在ProcessLocation類中,進行如下修改:

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

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

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();
    } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM