-2
這裏是LOG:Android的工作室:Android的運行時致命異常
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chong.medicinereminder, PID: 3221
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chong.medicinereminder/com.example.chong.medicinereminder.AddMedicine}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.chong.medicinereminder.AddMedicine.onCreate(AddMedicine.java:36)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
我真的需要幫助解決這個問題,因爲我所有的語法似乎沒有錯誤可言,請幫助我做一個醫學提醒應用程序和應用程序崩潰每次我按下'AddMedicine'按鈕。下面是問題的接口的編碼:
public class AddMedicine extends AppCompatActivity {
EditText med_name, med_reason, med_dosage;
List<MedicineList> MedicineList = new ArrayList<MedicineList>();
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_medicine);
med_name = (EditText) findViewById(R.id.text_med_name);
med_reason = (EditText) findViewById(R.id.text_med_reason);
med_dosage = (EditText) findViewById(R.id.text_med_dosage);
final Button button_save = (Button) findViewById(R.id.button_save);
button_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveMedicine(med_name.getText().toString(), med_reason.getText().toString(), med_dosage.getText().toString());
Toast.makeText(getApplicationContext(), med_name.getText().toString() + " has been added to the list", Toast.LENGTH_SHORT).show();
}
});
med_name.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
button_save.setEnabled(!med_name.getText().toString().trim().isEmpty());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private void saveMedicine(String name, String reason, String dosage) {
MedicineList.add(new MedicineList(name, reason, dosage));
}
private class MedicineListAdapter extends ArrayAdapter<MedicineList> {
public MedicineListAdapter() {
super(AddMedicine.this, R.layout.medicine_list, MedicineList);
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null)
view = getLayoutInflater().inflate(R.layout.medicine_list, parent, false);
MedicineList currentMedicine = MedicineList.get(position);
TextView name = (TextView) view.findViewById(R.id.medicine_name);
name.setText(currentMedicine.getName());
TextView reason = (TextView) view.findViewById(R.id.medicine_reason);
reason.setText(currentMedicine.getReason());
TextView dosage = (TextView) view.findViewById(R.id.medicine_dosage);
dosage.setText(currentMedicine.getDosage());
return view;
}
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_medicine_add, menu);
return true;
}
}
確保您的activity_add_medicine' XML文件中有一個id名稱'button_save'。 – Real73