gpt4 book ai didi

android - 尝试在空对象引用上调用虚方法 'void android.widget.EditText.setText

转载 作者:行者123 更新时间:2023-11-30 04:54:04 25 4
gpt4 key购买 nike

<分区>


我遇到了这个错误


尝试在空对象引用上调用虚方法'void android.widget.EditText.setText(java.lang.CharSequence, android.widget.TextView$BufferType)'


当我尝试使用 setText 方法将一些数据设置到 EditText 中时,它指向 null 并使应用程序关闭,下面的照片显示即使我设置了数据,编辑文本也是 null:


enter image description here


请多多指教。谢谢!

更新我的代码:


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) {

}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com