我有一个有效的对话框,直到添加代码以尝试启动另一个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");