作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<分区>
我遇到了这个错误
尝试在空对象引用上调用虚方法'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) {
}
}
我有一个特别的问题想要解决,我不确定是否可行,因为我找不到任何信息或正在完成的示例。基本上,我有: class ParentObject {}; class DerivedObject : publi
在我们的项目中,我们配置了虚 URL,以便用户可以在地址栏中输入虚 URL,这会将他们重定向到原始 URL。 例如: 如果用户输入'http://www.abc.com/partner ',它会将它们
我是一名优秀的程序员,十分优秀!