2013-10-28 58 views
0

當我單擊我的按鈕時,出現NullpointerException錯誤。 的logcat中表示該錯誤是在該行62單擊按鈕時出現NullPointerException

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\\.-][email protected]([\\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(""); 
} 

}

+0

檢查您的Title.getText()爲空值。 – RTA

+1

您的代碼非常難以閱讀。請使用小寫字母作爲變量和方法名的第一個字母(例如'title',而不是'Title'),大寫字母表示類和接口的首字母。 'private EditText title,description,coordonnees,tel,mail;' – Simon

回答

2

你應該INFL在查找元素之前先吃掉你的佈局。

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

doc

幾乎所有的活動,與用戶進行交互,所以Activity類 需要爲您創建一個窗口,在其中您可以將您的UI 與setContentView(View)

的護理

/**/

onCreate(Bundle)是你ini調整你的活動。大多數 重要的是,在這裏你會通常所說setContentView(int)與 佈局資源定義你的用戶界面,並使用findViewById(int)到 檢索該UI,你需要與 程序交互的小部件。

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.setOnClickListener(this); 
+0

我的代碼就像你說的,我在findViewById語句之前有setContentView語句,但在這種情況下,我在logcat中出現錯誤。 我認爲問題不在setContentView語句中,因爲它在我的android手機上正常工作,但是一旦我點擊該按鈕,就會得到NPE錯誤。 我試圖讓你的logcat,它會在幾分鐘內準備好 –

+0

@ahmedbenfadhel檢查xml中的ID。如果您在初始化視圖的時候正確引用了它們,那麼就是chck。 – Raghunandan

+0

就是這樣,我在一些ID中有一些錯誤,現在它可以正常工作,謝謝 –

2

你需要你的intialization前使用setContentView所以更換此

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

通過的意見。

始終保持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); 
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關聯到您的按鈕。

相關問題