0

单击按钮时出现 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

4 回答 4

2

您需要setContentView在初始化视图之前使用。

始终保持setContentViewsuper.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);
于 2013-10-28T13:03:06.313 回答
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在当前的膨胀布局中查找视图。所以你需要先将布局的内容设置到activity中,然后再初始化你的views。

您已经像下面这样初始化了 imageView 不需要再次初始化它

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

所以替换这个

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

经过

  imageView.setOnClickListener(this);
于 2013-10-28T13:02:03.670 回答
2

在找到元素之前,您应该为您的布局充气。

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

文档

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

/**/

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

于 2013-10-28T13:01:22.940 回答
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 关联到您的按钮。

于 2013-10-28T13:07:21.110 回答