Android Studio - 我无法在实时数据库Fire base中使用intent

时间:2018-05-02 00:31:00

标签: android firebase android-intent firebase-realtime-database

我尝试使用之前活动的意图来查看来自我的数据库的信息,但是当我尝试这样做时,我收到此错误“java.lang.NullPointerException:无法为子句中的参数'pathString'传递null( )“所以我不知道如何继续,这就是我的代码:

package es.ua.eps.automatismos;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Space;
import android.widget.TextView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import org.w3c.dom.Text;

import java.util.concurrent.TimeUnit;

public class Saldo extends AppCompatActivity {
    private TextView mTV;
    String spLine;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_saldo);
        Intent I = getIntent();
         spLine = I.getStringExtra("SC");

    mTV = (TextView) findViewById(R.id.saldo);
}
    DatabaseReference mDBR = FirebaseDatabase.getInstance().getReference();
    DatabaseReference mRC = mDBR.child(spLine);
    @Override
    protected void onStart() {
        super.onStart();

        mRC.addValueEventListener(new ValueEventListener() {
            @Override

            public void onDataChange(DataSnapshot dataSnapshot) {
                String Money = dataSnapshot.getValue().toString();
                mTV.setText(Money);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

您在onCreate()之外调用DatabaseReference mRC = mDBR.child(spLine),因此尚未初始化spLine。您可以像这样重写代码:

public class Saldo extends AppCompatActivity {
        private TextView mTV;
        String spLine;
        DatabaseReference mDBR;
        DatabaseReference mRC;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_saldo);

            Intent I = getIntent();
            spLine = I.getStringExtra("SC");

            mDBR = FirebaseDatabase.getInstance().getReference();
            mRC = mDBR.child(spLine);

            mTV = (TextView) findViewById(R.id.saldo);
    }

        @Override
        protected void onStart() {
            super.onStart();

            mRC.addValueEventListener(new ValueEventListener() {
                @Override

                public void onDataChange(DataSnapshot dataSnapshot) {
                    String Money = dataSnapshot.getValue().toString();
                    mTV.setText(Money);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    }