我正在创建一个应用程序,我将数据从一个片段发送到对话框片段 通过Bundle并使用哈希映射传递该字符串并将该数据保存到数据库。当我从片段发送数据到对话框片段时,它没有到达那里我得到以下错误,我的应用程序崩溃:
Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
片段代码:
private void mobileNumber()
{
Bundle args = new Bundle();
args.putString("mobile", edtMobileNumber.getText().toString());
DialogFragment newFragment = new Save_contact_fragment();
newFragment.setArguments(args);
// newFragment.show(getFragmentManager(),"TAG");
}
在此代码中,我将向对话框片段发送手机号码。对话框片段代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TokenModel mTokenModel = ((BaseActivity)getActivity()).getTokenDetails();
if (mTokenModel != null) {
mAuthorizationHeader = mTokenModel.getTokenType() + " " + mTokenModel.getAccessToken();
}
Bundle args = this.getArguments();
data = args.getString("mobile");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.activity_save_contact, container, false);
initUI(view);
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
/*Bundle bundle = this.getArguments();
if (bundle == null) {
data = bundle.getString("mobile");
}*/
}
@Override
public void onDetach() {
super.onDetach();
}
private void initUI(View view) {
btnsave = (Button) view.findViewById(R.id.save);
btnCancelDialog = (Button) view.findViewById(R.id.btn_cancel);
etPromoCode = (EditText) view.findViewById(R.id.et_promo_code);
etPromoCode.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.toString().length() > 0) {
btnsave.setEnabled(true);
} else {
btnsave.setEnabled(false);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
// Should always be at the end
bindClicks();
}
private void bindClicks() {
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
callAddToContactAPI();
//Toast.makeText(getContext(), "save", Toast.LENGTH_SHORT).show();
//dismiss();
}
});
btnCancelDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
}
private void callAddToContactAPI() {
if (selectedPlan == null) {
selectedPlan = new SaveContactModel();
//selectedPlan.setRemark(Constants.CATEGORY_MOBILE_RECHARGE);
//Toast.makeText(getContext(), "1", Toast.LENGTH_SHORT).show();
}
//selectedPlan.setPlanMrp(Integer.parseInt(Utils.getTextFromEditText(etPromoCode)));
addToContactAPI(selectedPlan);
}
private void addToContactAPI(SaveContactModel saveContactModel) {
if (!Utils.isNetworkConnected(getActivity())) {
return;
}
CartClient cartClient = ServiceGenerator.createService(CartClient.class);
HashMap<String, Object> parameterMap = new HashMap<>();
parameterMap.put(Constants.ADD_ContactName,Utils.getTextFromEditText(etPromoCode));
parameterMap.put(Constants.ADD_ContactNumber,data);
parameterMap.put(Constants.ADD_ServiceID_TO_SAVE, 1);
parameterMap.put(Constants.ADD_UserBasicID,"1593");
Call<String> cart = cartClient.addToContact(mAuthorizationHeader, parameterMap);
//Toast.makeText(getContext(), "2", Toast.LENGTH_SHORT).show();
String abc = cart.request().url().toString();
cart.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if(response.code() == 200) {
Intent intent = new Intent(getContext(), MyContactActivity.class);
startActivity(intent);
Utils.showAlertDialogWithMessage(Constants.CONTACT_ADDED, getActivity());
// Utils.showAlertDialogWithMessage(Constants.SUCCESS_MESSAGE_ITEM_ADDED_TO_CART, getActivity());
} else {
// Utils.showAlertDialogWithMessage(Constants.ERROR_UNABLE_TO_PROCESS_REQUEST, getActivity());
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Utils.showShortToastMessage(getActivity(), Constants.ERROR_OOPS_SOMETHING_WENT_WRONG);
}
});
}
这是我在对话框片段中接收数据的地方:
Bundle args = this.getArguments();
data = args.getString("mobile");
但是当我将该数据字符串传递给哈希映射时,我得到null并得到错误。我在以下代码中这样做:
HashMap<String, Object> parameterMap = new HashMap<>();
parameterMap.put(Constants.ADD_ContactName,Utils.getTextFromEditText(etPromoCode));
parameterMap.put(Constants.ADD_ContactNumber,data);
parameterMap.put(Constants.ADD_ServiceID_TO_SAVE, 1);
parameterMap.put(Constants.ADD_UserBasicID,"1593");
手机号码不在手机中。
答案 0 :(得分:0)
把这部分:
Bundle args = getArguments();
data = args.getString("mobile");
在onCreateView
而不是onCreate
而没有this.
答案 1 :(得分:0)
您可以使用onViewCreated
或onCreateView
方法获取传递的参数。如果您没有收到包,则如果参数不同于null
,则将使用默认字符串。
String mobile = getArguments() != null ? getArguments().getString("mobile", "defaultString") : "defaultString";