提问人:zuni 提问时间:1/6/2023 最后编辑:Bokenzuni 更新时间:1/6/2023 访问量:78
将数据从一个片段发送到另一个片段时,获取 Bundle.getString 的空指针异常
getting null pointer exception for Bundle.getString while sending data from one fragment to another fragment
问:
我正在尝试使用捆绑包将数据从一个片段发送到另一个片段,但它向我显示此错误:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.String android.os.Bundle.getString(java.lang.String)”
这是我的注册片段
public class signupFragment extends Fragment {
private AutoCompleteTextView autoCompleteTextView;
private EditText name,address,phone,alt_phone;
private CountryCodePicker ccp;
private ImageView user_dp;
private Button signup_btn;
private NavController navController;
private Uri imageUri;
public signupFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layoutSignup= inflater.inflate(R.layout.fragment_signup, container, false);
((AppCompatActivity)getActivity()).getSupportActionBar().hide();
return layoutSignup;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
autoCompleteTextView = view.findViewById(R.id.auto_signup);
name = view.findViewById(R.id.name_signup);
address = view.findViewById(R.id.address_signup);
phone = view.findViewById(R.id.phone_signup);
alt_phone = view.findViewById(R.id.alt_phone_signup);
ccp = view.findViewById(R.id.ccp_signup);
user_dp = view.findViewById(R.id.img_signup);
signup_btn = view.findViewById(R.id.btn_signup);
navController = Navigation.findNavController(view);
ccp.registerCarrierNumberEditText(phone);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(), R.array.user_type, R.layout.dropdown_register);
autoCompleteTextView.setAdapter(adapter);
signup_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putString("name",name.getText().toString());
bundle.putString("phone",ccp.getFullNumberWithPlus().replace(" ",""));
bundle.putString("user_type",autoCompleteTextView.getText().toString());
bundle.putString("alt_phone",alt_phone.getText().toString());
bundle.putString("address",address.getText().toString());
bundle.putString("dp_url",user_dp.getResources().toString());
otpFragment OtpFragment = new otpFragment();
OtpFragment.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.fragmentContainerView2,OtpFragment).commit();
navController.navigate(R.id.action_signupFragment_to_otpFragment);
}
});
user_dp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setimage();
}
});
}
// This is to select the image from the mobile
private void setimage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,100);
}
// This is to set the image and to check if we got an image or not
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==100 && data!=null && data.getData()!=null){
imageUri = data.getData();
user_dp.setImageURI(imageUri);
}
}
}
这是我的otp片段
public class otpFragment extends Fragment {
private Button verify_otp_btn;
private EditText edt_otp;
private TextView resend_otp;
private FirebaseAuth mAuth;
private String dp_url="";
private String name="";
private String phoneNumber="";
private String otpid="";
private String user_type="";
private String user_id="";
private String alt_phone="";
private String address="";
private NavController navController;
// firebase declaration
private FirebaseFirestore firestore;
public otpFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layoutOtp= inflater.inflate(R.layout.fragment_otp, container, false);
((AppCompatActivity)getActivity()).getSupportActionBar().hide();
return layoutOtp;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
verify_otp_btn= view.findViewById(R.id.verify_otp_btn);
edt_otp= view.findViewById(R.id.code_otp);
resend_otp=view.findViewById(R.id.resend_otp);
navController= Navigation.findNavController(view);
Bundle result;
result = this.getArguments();
name = result.getString("name");
phoneNumber = result.getString("phone");
user_type = result.getString("user_type");
alt_phone = result.getString("alt_phone");
address = result.getString("address");
dp_url = result.getString("dp_url");
Log.d("phone no",phoneNumber+" this is ");
Log.d("phone no",name+" this is ");
Log.d("phone no",user_type+" this is ");
Log.d("phone no",alt_phone+" this is ");
Log.d("phone no",address+" this is ");
Log.d("phone no",dp_url+" this is ");
verify_otp();
}
答: 暂无答案
评论