尝试在空对象引用上调用虚方法'void android.widget.EditText.setText
Attempt to invoke virtual method 'void android.widget.EditText.setText on a null object reference
我遇到了这个错误
尝试在空对象引用上调用虚拟方法'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) {
}
}
}
您在 AddUserDialog class 构造函数中的 loadUserData() 方法中调用 EditText 上的 setText() 方法。但是 findViewById() 方法位于 initialize() 方法中的 onCreateView() 回调方法中。
由于构造方法在 onCreateView() 回调方法之前被调用,loadUserData() 方法在 initialize() 之前被调用,这意味着 setText() 方法在 EditTexts 通过 findViewById() 方法与 UI 绑定之前被调用。
您可以尝试在 onCreateView() 回调或单独的 onCreate() 回调中替换 editTextName.setText() 方法。因此 EditTexts 首先与 UI 绑定,然后在 EditText 对象上调用 setText() 方法。
我遇到了这个错误
尝试在空对象引用上调用虚拟方法'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) {
}
}
}
您在 AddUserDialog class 构造函数中的 loadUserData() 方法中调用 EditText 上的 setText() 方法。但是 findViewById() 方法位于 initialize() 方法中的 onCreateView() 回调方法中。
由于构造方法在 onCreateView() 回调方法之前被调用,loadUserData() 方法在 initialize() 之前被调用,这意味着 setText() 方法在 EditTexts 通过 findViewById() 方法与 UI 绑定之前被调用。
您可以尝试在 onCreateView() 回调或单独的 onCreate() 回调中替换 editTextName.setText() 方法。因此 EditTexts 首先与 UI 绑定,然后在 EditText 对象上调用 setText() 方法。