2016-02-25 81 views
-1

我只是想setOnClickListener圖像按鈕上的方法,它會導致空指針異常錯誤。如下圖所示NullPointerException:與OnClickListener啓動方法

的logcat:

02-25 10:49:43.051 13936-13936/com.example.l33902.contactmanagment1512 E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.example.l33902.contactmanagment1512, PID: 13936 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.l33902.contactmanagment1512/com.example.l33902.contactmanagment.EditActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object referenceat android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3155) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3263) 
    at android.app.ActivityThread.access$1000(ActivityThread.java:197) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1687) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:145) 
    at android.app.ActivityThread.main(ActivityThread.java:6897) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 
    at com.example.l33902.contactmanagment.EditActivity.onCreate(EditActivity.java:158) 
    at android.app.Activity.performCreate(Activity.java:6550) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3108) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3263)  
    at android.app.ActivityThread.access$1000(ActivityThread.java:197)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1687)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:145)  
    at android.app.ActivityThread.main(ActivityThread.java:6897)  
    at java.lang.reflect.Method.invoke(Native Method)  
    at java.lang.reflect.Method.invoke(Method.java:372)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)  

以下是我的相關代碼:

ImageButton saveButton; 

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.edit_activity); 

saveButton = (ImageButton) findViewById(R.id.edit_activity_save); 
displayNameEditText = (EditText) findViewById(R.id.edit_activity_text_cal_name); 
    displayNameEditTextLyout = (TextInputLayout) findViewById(R.id.edit_activity_text_cal_name_layout); 

saveButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       save(); 
      } 
     }); 

} 

private void save() { 
     if (displayNameEditText.getText().length() == 0) { 
      displayNameEditTextLyout.setError(getString(R.string.edit_activity_error_empty_name)); 
     } else { 
      displayNameEditTextLyout.setError(null); 
      if (edit) 
       updateCalendar(); 
      else 
       addCalendar(EditActivity.this); 
     } 
    } 

private void updateCalendar() { 
     CalendarController.updateCalendar(mCalendarId, displayNameEditText.getText() 
       .toString(), colorPicker.getColor(), getContentResolver()); 
     finish(); 
    } 

在我的佈局XML:

<ImageButton 
        android:id="@+id/edit_activity_save" 
        android:layout_width="64dp" 
        android:layout_height="64dp" 
        android:contentDescription="@string/edit_activity_save" 
        android:src="@drawable/ic_save_white_24dp"/> 

我想不出有什麼錯誤約我似乎已經宣佈並將一切指向正確的方法。

+0

你真的在你的xml中有id爲'edit_activity_save'的按鈕嗎? –

回答

0

在您的代碼中,displayNameEditText未初始化,您可以先初始化視圖,然後再使用。

0

您需要在xml佈局中檢查Image buttin的身份。

saveButton = (ImageButton) findViewById(R.id.edit_activity_save); 

也許你似乎使用了錯誤的身份。

有關更多詳細信息,請參閱android文檔。

http://developer.android.com/reference/android/app/Activity.html#findViewById(int)

findViewById函數返回空值,如果沒有找到視圖。

享受良好的編碼!

+0

是的,我在我的XML佈局 –

+0

毫米,你有沒有嘗試重新編譯後,清理所有? – covernal

0

試試這個糾正代碼:

ImageButton saveButton; 
EditText displayNameEditText //declare it first 
EditText displayNameEditTextLyout //if it's an EditText 


@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.edit_activity); 

saveButton = (ImageButton) findViewById(R.id.edit_activity_save); 
//add this line 
displayNameEditText = (EditText) findViewById(R.id.your_particular_xml_id); //your_particular_xml_id must exist in your xml file 
//add this line 
displayNameEditTextLyout = (EditText) findViewById(R.id.particular_xml_id); // particular_xml_id must exist in your xml file 

saveButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       save(); 
      } 
     }); 

} 

private void save() { 
     if (displayNameEditText.getText().length() == 0) { 
      displayNameEditTextLyout.setError(getString(R.string.edit_activity_error_empty_name)); 
     } else { 
      displayNameEditTextLyout.setError(null); 
      if (edit) 
       updateCalendar(); 
      else 
       addCalendar(EditActivity.this); 
     } 
    } 

private void updateCalendar() { 
     CalendarController.updateCalendar(mCalendarId, displayNameEditText.getText() 
       .toString(), colorPicker.getColor(), getContentResolver()); 
     finish(); 
    } 

我假設你還定義addCalendar在code.If不是()方法,你要因爲你已經在save()方法中使用它。

我希望它有幫助。

相關問題