我遇到了这个错误
试图在空对象引用上调用虚拟方法'void android.widget.EditText.setText(java.lang.CharSequence,android.widget.TextView $ BufferType)'
当我尝试使用setText方法将一些数据设置为EditText时,但它指向null并使应用程序关闭,下图显示即使我设置了数据,编辑文本也为null:
请告诉我。 谢谢!
更新我的代码:
enter code here : public class AddUserDialog extends DialogFragment {
public interface OnInputUserTypeListener {
void sendInputAdd(UserInfoModel userInfoModel);
void sendInputUpdate(int position, UserInfoModel userInfo);
}
public OnInputUserTypeListener onInputUserTypeListener;
private int PICK_IMAGE_REQUEST = 1;
public Button add;
public EditText editTextName, editTextEmail, editTextPhone;
public CircleImageView imageViewaddPhoto;
public TextView cancel;
public Bitmap bitmap;
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
UserInfoModel userInfo;
int position=-1;
String type;
Uri uri;
public AddUserDialog(String type) {
this.type = type;
}
public AddUserDialog(int position,UserInfoModel userInfo ,String type) {
this.userInfo = userInfo;
this.type = type;
this.position = position;
loadUserData();
}
public AddUserDialog(int addposition,String addtype) {
this.type = addtype;
this.position = addposition;
}
public void loadUserData() {
String phone = userInfo.getPhone();
String name = userInfo.getName();
String email = userInfo.getEmail();
// Bitmap photo = userInfo.getPhoto();
editTextName.setText(name,TextView.BufferType.EDITABLE);
editTextPhone.setText(phone,TextView.BufferType.EDITABLE);
editTextEmail.setText(email,TextView.BufferType.EDITABLE);
// imageViewaddPhoto.setImageBitmap(photo);
}
@NonNull
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.add_user_dialog, container, false);
initialize(v);
listeners();
return v;
}
public void initialize(View v) {
add = v.findViewById(R.id.button);
cancel = v.findViewById(R.id.cancel_dialog);
editTextName = v.findViewById(R.id.name);
editTextEmail = v.findViewById(R.id.email);
editTextPhone = v.findViewById(R.id.phone);
imageViewaddPhoto = v.findViewById(R.id.imageView);
}
public void listeners() {
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDialog().dismiss();
}
});
imageViewaddPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chooseImage();
}
});
add.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(editTextName.getText())) {
editTextName.setError(getResources().getString(R.string.required));
editTextName.requestFocus();
return;
}
if (TextUtils.isEmpty(editTextPhone.getText())) {
editTextPhone.setError(getResources().getString(R.string.required));
editTextPhone.requestFocus();
return;
}
if (TextUtils.isEmpty(editTextEmail.getText())) {
editTextEmail.setError(getResources().getString(R.string.required));
editTextEmail.requestFocus();
return;
}
if (editTextEmail.getText().toString().trim().matches(emailPattern)) {
editTextEmail.requestFocus();
} else {
editTextEmail.setError(getResources().getString(R.string.invalid));
editTextEmail.requestFocus();
return;
}
String name = editTextName.getText().toString();
String email = editTextEmail.getText().toString();
String phone = editTextPhone.getText().toString();
imageViewaddPhoto.setDrawingCacheEnabled(true);
if (!name.isEmpty() || (!phone.isEmpty() || !email.isEmpty())) {
if (type.equals("new")) {
UserInfoModel u1 = new UserInfoModel(name, email, phone, bitmap);
onInputUserTypeListener.sendInputAdd(u1);
Snackbar.make(v, "User added", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
Objects.requireNonNull(getDialog()).dismiss();
}
if (type.equals("update")) {
UserInfoModel u1 = new UserInfoModel(name, email, phone, bitmap);
onInputUserTypeListener.sendInputUpdate(position, u1);
Snackbar.make(v, "User updated", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
Objects.requireNonNull(getDialog()).dismiss();
}
}
}
});
}
public void chooseImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
uri = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getApplicationContext().getContentResolver(), uri);
imageViewaddPhoto.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
onInputUserTypeListener = (OnInputUserTypeListener) getActivity();
} catch (ClassCastException e) {
}
}
}
答案 0 :(得分:0)
您正在位于AddUserDialog类构造函数内的loadUserData()方法内的EditTexts上调用setText()方法。但是findViewById()方法位于initialize()方法内的onCreateView()回调方法内。
由于在onCreateView()回调方法之前调用了构造函数方法,因此在initialize()之前调用了loadUserData()方法,这意味着在EditTexts通过findViewById()方法与UI绑定之前,将调用setText()方法。
您可以尝试在onCreateView()回调中或在单独的onCreate()回调中替换editTextName.setText()方法。因此,EditText首先与UI绑定,然后在EditText对象上调用setText()方法。