单击按钮时出现NullPointerException

时间:2013-10-28 13:00:39

标签: java android

单击我的按钮时出现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("");
}

}

4 个答案:

答案 0 :(得分:2)

在找到元素之前,您应该对布局进行充气。

 super.onCreate(savedInstanceState);    
 setContentView(R.layout.activity_main); <-- move this line here
 Title = (EditText) findViewById(R.id.titre);
 /**/
  //initFields();

来自doc

  

几乎所有活动都与用户交互,因此Activity类   负责为您创建一个窗口,您可以在其中放置U​​I   与setContentView(View)

     

/**/

     

onCreate(Bundle)是您初始化活动的地方。最   重要的是,在这里你通常会打电话给setContentView(int)   布局资源定义您的UI,并使用findViewById(int)来   检索该UI中需要与之交互的小部件   编程。

答案 1 :(得分:2)

尝试以下

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Title = (EditText) findViewById(R.id.titre);
    ... // rest of the code  

您需要先扩充布局然后初始化视图。 findViewById在当前膨胀的布局中查找视图。因此,您需要先将布局内容设置为活动,然后初始化视图。

您已经初始化了imageView,如下所示,无需再次初始化

  imageView = (ImageView) findViewById(R.id.images);

所以替换这个

  ((ImageView)this.findViewById(R.id.images)).setOnClickListener(this);

通过

  imageView.setOnClickListener(this);

答案 2 :(得分:2)

您需要在初始化视图之前使用setContentView。始终在setContentView之后保留super.onCreate(savedInstanceState);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


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

答案 3 :(得分:0)

正如一些用户所说,你必须在从中获取视图之前创建和设置布局。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

然后你可以将onClickListener与你的按钮相关联。