-3

I am using a Firebase database and Android Studio. When I run the app and click the add food button the app crashes and nothing is added to the db. If I don't enter a food name the error message comes up as expected.

Here is my code:

public class addFood extends AppCompatActivity {

//source : https://www.youtube.com/watch?v=EM2x33g4syY&t=415s
EditText editTextName;
EditText editTextBestBefore;
Button buttonAddFood;
Spinner spinnerCategory;

DatabaseReference foodDatabase;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_food);

    // source : https://www.youtube.com/watch?v=EM2x33g4syY&t=415s
    foodDatabase = FirebaseDatabase.getInstance().getReference("food");

    editTextName =(EditText)(findViewById(R.id.editTextName));
    editTextBestBefore =(EditText)(findViewById(R.id.editTextBestBefore));
    buttonAddFood =(Button) (findViewById(R.id.buttonAddFood));
    spinnerCategory =(Spinner) (findViewById(R.id.spinnerCategory));

    buttonAddFood.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addFood();
        }
    });

}

private void addFood()
        //source : https://www.youtube.com/watch?v=EM2x33g4syY&t=415s
{
    String foodName = editTextName.getText().toString().trim();
    String bestBefore = editTextBestBefore.getText().toString().trim();
    String category = spinnerCategory.getSelectedItem().toString();

    if (!TextUtils.isEmpty(foodName)){
        String id = foodDatabase.push().getKey();

        food food = new food(id,foodName, bestBefore, category);

        foodDatabase.child(id).setValue(food);
        Toast.makeText(this, "food added!", Toast.LENGTH_LONG).show();
    }
    else{
        Toast.makeText(this, "Please enter a food name", Toast.LENGTH_LONG).show();
    }
}

}

Logcat:

01-16 15:28:57.972 27506-27506/com.virtual.a2018fridge I/FA: App measurement is starting up, version: 11020 01-16 15:28:57.973 27506-27506/com.virtual.a2018fridge I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE 01-16 15:28:58.032 27506-27506/com.virtual.a2018fridge V/FA: Collection enabled 01-16 15:28:58.033 27506-27506/com.virtual.a2018fridge V/FA: App package, google app id: com.virtual.a2018fridge, 1:203952876063:android:ae6871bb1651394b 01-16 15:28:58.035 27506-27506/com.virtual.a2018fridge I/FA: To enable faster debug mode event logging run: adb shell setprop debug.firebase.analytics.app com.virtual.a2018fridge 01-16 15:28:58.035 27506-27506/com.virtual.a2018fridge D/FA: Debug-level message logging enabled 01-16 15:28:58.145 27506-27506/com.virtual.a2018fridge V/FA: Registered activity lifecycle callback 01-16 15:28:58.272 27506-27625/com.virtual.a2018fridge V/FA: Using measurement service 01-16 15:28:58.287 27506-27625/com.virtual.a2018fridge V/FA: Connecting to remote service 01-16 15:28:58.348 27506-27625/com.virtual.a2018fridge V/FA: Using measurement service 01-16 15:28:58.348 27506-27625/com.virtual.a2018fridge V/FA: Connection attempt already in progress 01-16 15:28:59.474 27506-27506/com.virtual.a2018fridge V/FA: onActivityCreated 01-16 15:29:01.984 27506-27625/com.virtual.a2018fridge V/FA: Using measurement service 01-16 15:29:01.984 27506-27625/com.virtual.a2018fridge V/FA: Connection attempt already in progress 01-16 15:29:01.989 27506-27625/com.virtual.a2018fridge V/FA: Activity resumed, time: 329370286 01-16 15:29:01.993 27506-27625/com.virtual.a2018fridge I/FA: Tag Manager is not found and thus will not be used 01-16 15:29:02.001 27506-27625/com.virtual.a2018fridge D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=addFood, firebase_screen_id(_si)=3652847265616890340}] 01-16 15:29:02.030 27506-27625/com.virtual.a2018fridge V/FA: Using measurement service 01-16 15:29:02.030 27506-27625/com.virtual.a2018fridge V/FA: Connection attempt already in progress 01-16 15:29:03.231 27506-27625/com.virtual.a2018fridge D/FA: Connected to remote service 01-16 15:29:03.232 27506-27625/com.virtual.a2018fridge V/FA: Processing queued up service tasks: 4 01-16 15:29:03.981 17286-27703/? V/FA-SVC: Logging event: origin=auto,name=screen_view(_vs),params=Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=addFood, firebase_screen_id(_si)=3652847265616890340}] 01-16 15:29:04.047 17286-27703/? V/FA-SVC: Saving event, name, data size: screen_view(_vs), 52 01-16 15:29:04.057 17286-27703/? V/FA-SVC: Event recorded: Event{appId='com.virtual.a2018fridge', name='screen_view(_vs)', params=Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=addFood, firebase_screen_id(_si)=3652847265616890340}]} 01-16 15:29:04.063 17286-27703/? V/FA-SVC: Upload scheduled in approximately ms: 978689 01-16 15:29:04.097 17286-27703/? V/FA-SVC: Scheduling upload with GcmTaskService 01-16 15:29:04.098 17286-27703/? V/FA-SVC: Scheduling task with Gcm. Time978689 01-16 15:29:04.106 17286-27703/? V/FA-SVC: Background event processing time, ms: 133 01-16 15:29:07.348 27772-27772/? I/FA: App measurement is starting up, version: 11020 01-16 15:29:07.349 27772-27772/? I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE 01-16 15:29:07.370 27772-27772/? V/FA: Collection enabled 01-16 15:29:07.371 27772-27772/? V/FA: App package, google app id: com.whatsapp, 1:293955441834:android:7373a2d0bdfa3228 01-16 15:29:07.372 27772-27772/? I/FA: To enable faster debug mode event logging run: adb shell setprop debug.firebase.analytics.app com.whatsapp 01-16 15:29:07.372 27772-27772/? D/FA: Debug-level message logging enabled 01-16 15:29:07.388 27772-27772/? V/FA: Registered activity lifecycle callback 01-16 15:29:07.415 27772-27818/? V/FA: Using measurement service 01-16 15:29:07.419 27772-27818/? V/FA: Connecting to remote service 01-16 15:29:07.439 27772-27818/? V/FA: Using measurement service 01-16 15:29:07.439 27772-27818/? V/FA: Connection attempt already in progress 01-16 15:29:08.026 27772-27818/? D/FA: Connected to remote service 01-16 15:29:08.026 27772-27818/? V/FA: Processing queued up service tasks: 2 01-16 15:29:09.150 27506-27625/com.virtual.a2018fridge V/FA: Inactivity, disconnecting from the service

                                                         [ 01-16 15:29:09.339 27506:27506 W/         ]
                                                         Unable to open '/system/framework/prcui-config.jar': No such file or directory

01-16 15:29:11.535 17286-27791/? D/FA-SVC: Got package replaced intent: com.virtual.a2018fridge

fhsks
  • 15
  • 1
  • 6

1 Answers1

1

you didn't added the code which will structure your database

like for example something like this

mRootRef.child("Chat").child(mCurrentUserId).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            if(!dataSnapshot.hasChild(mChatUser)){

                Map chatAddMap = new HashMap();
                chatAddMap.put("seen", false);
                chatAddMap.put("timestamp", ServerValue.TIMESTAMP);

                Map chatUserMap = new HashMap();
                chatUserMap.put("Chat/" + mCurrentUserId + "/" + mChatUser, chatAddMap);
                chatUserMap.put("Chat/" + mChatUser + "/" + mCurrentUserId, chatAddMap);

                mRootRef.updateChildren(chatUserMap, new DatabaseReference.CompletionListener() {
                    @Override
                    public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {

                        if(databaseError != null){

                            Log.d("CHAT_LOG", databaseError.getMessage().toString());

                        }

                    }
                });

            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });



    mChatSendBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            sendMessage();

        }
    });



}

where in you have to structure your database according to your requirement

Nikhil Kerkar
  • 149
  • 2
  • 10
  • add this before your code **buttonAddFood.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addFood(); } }); }** and change the values of it according to your requirement – Nikhil Kerkar Jan 25 '18 at 12:04