Hier ist der LOG:Android Studio: Android Runtime Fatal Exception
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)
ich wirklich Hilfe benötigen, dieses Problem zu lösen, weil alle meine Syntax scheinen überhaupt keine Fehler zu haben, bitte helfen ich bin macht eine Medizinerinnerung App und die App stürzte jedes Mal, wenn ich den 'AddMedicine' Knopf drückte. Unten ist die Codierung von Problem-Schnittstelle:
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;
}
}
Stellen Sie sicher, dass Sie in Ihrer XML-Datei "activity_add_medicine" den ID-Namen 'button_save' haben. – Real73