简体   繁体   中英

Android App crashes when trying to store 3 strings from a child on a Firebase database when trying to read strings. No Authentication yet

I've already set up Firebase with the app using Tools -> Firebase and added the internet access to manifest. I'm trying to just get the strings from the child to be saved on the app so I can use them later on when I develop it further. I dont care about authentication yet so userID process is irrelevant as far as I gathered. The first part of the main file has the writing side and when everything else is commented out the app runs and works fine. So the problem is with reading.

What the Firebase Database looks like right now

// MainActivity.java:

    public class MainActivity extends AppCompatActivity {

        private DatabaseReference mDatabase;


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

            mDatabase = FirebaseDatabase.getInstance().getReference("Strings");


            Button sendBtn = (Button) findViewById(R.id.sendBtn);
            EditText stringText = (EditText) findViewById(R.id.stringText);
            TextView textView = (TextView) findViewById(R.id.textView);


            sendBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    FirebaseDatabase database = FirebaseDatabase.getInstance();
                    DatabaseReference myRef = database.getReference("message");

                    myRef.setValue("I can send text to this from Android App");
                }

            });


            mDatabase.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    showData(dataSnapshot);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }

        private void showData(DataSnapshot dataSnapshot) {
            StringExchange stringX = new StringExchange();
            stringX.setPhoneNum(dataSnapshot.getValue(StringExchange.class).getPhoneNum());
            stringX.setTrapID(dataSnapshot.getValue(StringExchange.class).getTrapID());
            stringX.setActive(dataSnapshot.getValue(StringExchange.class).getActive());
            Toast.makeText(getBaseContext(), "Strings Saved", Toast.LENGTH_SHORT).show();
        }
   }




 // class: StringExchange.java:
    package com.snaresense.firebase;

    /**
     * Created by Kevin on 10/21/2017.
     */

    public class StringExchange {
        String PhoneNum;
        String Active;
        String TrapID;

        public StringExchange(){
        }
        public String getPhoneNum(){
            return PhoneNum;
        }
        public void setPhoneNum(String PhoneNum){
            this.PhoneNum = PhoneNum;
        }
        public String getActive(){
            return Active;
        }
        public void setActive(String Active){
            this.Active = Active;
        }
        public String getTrapID(){
            return TrapID;
        }
        public void setTrapID(String TrapID){
            this.TrapID = TrapID;
        }
    }

Your showData function is bit messed up. Try changing it to:

 private void showData(DataSnapshot dataSnapshot) {
        StringExchange stringX = dataSnapshot.getValue(StringExchange.class);
        Toast.makeText(this, "Strings Saved", Toast.LENGTH_SHORT).show();
    }

If it still doesn't work, please elaborate what is the exact problem? Is it crashing? If yes then share the log. If its not working as expected then explain whats not working.

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



    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();

    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String Active = dataSnapshot.child("snaresense").child("strings").child("StringActive").getValue(String.class);
            String BatteryLevel = dataSnapshot.child("snaresense").child("strings").child("StringBatteryLevel").getValue(String.class);
            String TrapId = dataSnapshot.child("snaresense").child("strings").child("StringTrapId").getValue(String.class);
            String fireData = ("Hello. Trap: "+TrapId+" has been activated. The battery level is: "+BatteryLevel);
            String usrnum = dataSnapshot.child("snaresense").child("strings").child("StringPhoneNum").getValue(String.class);
            sendMsg (usrnum, fireData);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

The reading part works and the sendMsg part is just for sending text messages with the final app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM