My application is crashing when I press the button inside a tabbed activity for the second time.
It works fine the first time,but crashes on every alternative trial. This activity has two Edittext
, one add button, a listview
and a longclicklistener
for each listview
item.
public class Tab3pmedicines extends Fragment {
private EditText medname,meddosage;
private Button add,delete;
private FirebaseAuth firebaseauth;
private FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
ListView ListViewMedicine;
List<MedicineProfile> medicine;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab3pmedicines, container, false);
medname = (EditText)rootView.findViewById(R.id.medicinenameID);
meddosage = (EditText)rootView.findViewById(R.id.dosageID);
add = (Button) rootView.findViewById(R.id.addmedbtnID);
ListViewMedicine = (ListView) rootView.findViewById(R.id.listViewID);
medicine = new ArrayList<>();
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
String RegisteredUserID = currentUser.getUid();
databaseReference = FirebaseDatabase.getInstance().getReference().child(RegisteredUserID).child("Medicines");
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addmedicines();
}
});
ListViewMedicine.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
MedicineProfile medicines = medicine.get(position);
String medicineID = medicines.getMedid();
showPopup(medicineID);
return false;
}
});
return rootView;
}
@Override
public void onStart() {
super.onStart();
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
medicine.clear();
for (DataSnapshot medicinesnapshot: dataSnapshot.getChildren()){
MedicineProfile medicineP = medicinesnapshot.getValue(MedicineProfile.class);
medicine.add(medicineP);
}
MedicineList adapter = new MedicineList(Tab3pmedicines.this.getActivity(),medicine);
ListViewMedicine.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void showPopup(String medid){
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(Tab3pmedicines.this.getActivity());
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View dialogView = inflater.inflate(R.layout.activity_popup,null);
dialogBuilder.setView(dialogView);
final String id = medid;
final TextView message = (TextView) dialogView.findViewById(R.id.deleteviewID);
delete = (Button) dialogView.findViewById(R.id.delbtnID);
final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteMedicine(id);
alertDialog.dismiss();
}
});
}
private void deleteMedicine(String id1){
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
String RegisteredUserID = currentUser.getUid();
DatabaseReference dmedicine = FirebaseDatabase.getInstance().getReference().child(RegisteredUserID).child("Medicines").child(id1);
dmedicine.removeValue();
Toast.makeText(getActivity(),"Medicine deleted",Toast.LENGTH_SHORT).show();
}
private void addmedicines(){
String mname = medname.getText().toString().trim();
String dosage = meddosage.getText().toString().trim();
String id = databaseReference.push().getKey();
if(validate()){
MedicineProfile medicineProfile = new MedicineProfile(id,mname,dosage);
databaseReference.child(id).setValue(medicineProfile);
Toast.makeText(getActivity(),"Medicine added",Toast.LENGTH_SHORT).show();
}
}
private boolean validate(){
Boolean result = false;
String mediname = medname.getText().toString();
String medidos = meddosage.getText().toString();
if (mediname.isEmpty() || medidos.isEmpty()){
Toast.makeText(getActivity(),"Enter the medicine details",Toast.LENGTH_SHORT).show();
}
else {
result = true;
}
return result;
}
}
LOGCAT :-
enter code here
04-07 12:57:45.292 23551-23551/? E/Zygote: v2
04-07 12:57:45.292 23551-23551/? E/Zygote: accessInfo : 0
04-07 12:58:16.062 23551-23551/com.example.mylaptop.myapplicationnps E/ViewRootImpl: sendUserActionEvent() mView == null
04-07 12:58:17.562 23551-23551/com.example.mylaptop.myapplicationnps E/MotionRecognitionManager: mSContextService = null
04-07 12:58:17.562 23551-23551/com.example.mylaptop.myapplicationnps E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@dd1127d
04-07 12:58:22.642 23551-23551/com.example.mylaptop.myapplicationnps E/ViewRootImpl: sendUserActionEvent() mView == null
04-07 12:58:51.202 23551-23551/com.example.mylaptop.myapplicationnps E/MotionRecognitionManager: mSContextService = null
04-07 12:58:51.202 23551-23551/com.example.mylaptop.myapplicationnps E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@30bc1e5
04-07 12:58:56.702 23551-23551/com.example.mylaptop.myapplicationnps E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mylaptop.myapplicationnps, PID: 23551
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.view.LayoutInflater.from(LayoutInflater.java:234)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:178)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:163)
at com.example.mylaptop.myapplicationnps.MedicineList.<init>(MedicineList.java:0)
at com.example.mylaptop.myapplicationnps.Tab3pmedicines$4.onDataChange(Tab3pmedicines.java:142)
at com.google.android.gms.internal.zzegf.zza(Unknown Source)
at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source)
at com.google.android.gms.internal.zzeig.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
This is the logcat error we get when the app crashes after switching the tabs.