单击按钮时出现 NullpointerException 错误。logcat 表示错误在第 62 行
String Titre = Title.getText().toString();
我认为这是因为我的 EditTexts 没有初始化,这就是我创建 initFields() 方法的原因,但我仍然不知道在哪里调用它,因为无论我在哪里调用它,我都会得到 NullPointerException
这是我的代码:
public class MainActivity extends Activity implements
android.view.View.OnClickListener{
private EditText Title, Description, Coordonnees, Tel, mail;
private Bitmap photo;
private ImageView imageView;
//private PhotoPicker photoPicker;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Title = (EditText) findViewById(R.id.titre);
Description = (EditText) findViewById(R.id.Description);
imageView = (ImageView) findViewById(R.id.images);
Coordonnees = (EditText) findViewById(R.id.Coordonnees);
Tel = (EditText) findViewById(R.id.tel);
mail = (EditText) findViewById(R.id.Mail);
setContentView(R.layout.activity_main);
//initFields();
((Button)this.findViewById(R.id.bouton1)).setOnClickListener(this);
((ImageView)this.findViewById(R.id.images)).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.images:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
break;
case R.id.bouton1:
String Titre = Title.getText().toString();
String Desc = Description.getText().toString();
String Coord = Coordonnees.getText().toString();
String tel = Tel.getText().toString();
String Mailtxt = mail.getText().toString();
boolean trueMail = Pattern
.compile("^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$",
Pattern.CASE_INSENSITIVE).matcher(Mailtxt)
.matches();
boolean trueTel = Pattern.matches("^[+]?[0-9]{3,}$", tel);
if (imageView == null || !trueMail || !trueTel || Titre.equals("")
|| Desc.equals("") || Coord.equals("")){
Toast.makeText(this, R.string.missing_field, Toast.LENGTH_SHORT).show();
}
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
imageView = (ImageView) findViewById(R.id.images);
imageView.setImageDrawable(getResources().getDrawable(
R.drawable.register_photo));
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=16; //image will be 16x smaller than an original. Note, that it's better for perfomanse to use powers of 2 (2,4,8,16,32..).
Bitmap bmp = BitmapFactory.decodeFile(selectedImagePath, options);
Drawable d = new BitmapDrawable(bmp);
imageView.setImageDrawable(d);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
private void initFields() {
Title.setText("");
Description.setText("");
Coordonnees.setText("");
Tel.setText("");
mail.setText("");
}
}