空对象引用上的“ java.lang.String java.lang.Object.toString()”上的应用程序崩溃NullPointerException

时间:2018-08-28 11:19:44

标签: java android firebase firebase-realtime-database

打开视图几秒钟后,应用程序崩溃。 我从Firebase数据库获得的值(食物,活动,饮料)是我包含在数据库中的值。值存在,但似乎显示为空

这是我的代码

public class userView extends AppCompatActivity {

private FirebaseUserMetadata userMetadata;
private FirebaseDatabase firebaseDatabase;

private ArrayList<String> listLike;
private ArrayAdapter<String> adapter;

private ListView listView;


String uid;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_user_view );
    Toolbar toolbar = findViewById( R.id.toolbar );
    setSupportActionBar( toolbar );

    //SHOW USER INFO
    TextView eEmail = findViewById( R.id.email );
    TextView eUser = findViewById( R.id.username );

    listView = findViewById( R.id.listLikes );
    FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

    assert firebaseUser != null;
    uid = firebaseUser.getUid();

    //START ARRAY
    listLike = new ArrayList<>();

    eUser.setText( firebaseUser.getDisplayName());
    eEmail.setText( firebaseUser.getEmail() );

    ActionBar ab = getSupportActionBar();
    if (ab != null) {
        ab.setDisplayHomeAsUpEnabled( true );
    }
    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();

    //TODO get list of the things the user likes
    //Item from list era set
    databaseReference.addValueEventListener( new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            String user_food = dataSnapshot.child( uid ).child( "food" ).getValue( String.class );
            String user_drink = dataSnapshot.child( uid ).child( "drink" ).getValue( String.class );
            String user_activity = dataSnapshot.child( uid ).child( "activity" ).getValue( String.class );


            listLike.add( user_food );
            listLike.add( user_drink );
            listLike.add( user_activity );

            adapter = new ArrayAdapter<>( userView.this, android.R.layout.simple_list_item_1, listLike );
            listView.setAdapter( adapter );
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    } );
}

}

打开视图约3秒钟后出现错误。预制数据库的值。因此,应该在数组中显示变量。 它吐出此错误

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.joshua.toku, PID: 16025
              java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
                  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:445)
                  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:407)
                  at android.widget.AbsListView.obtainView(AbsListView.java:2437)
                  at android.widget.ListView.measureHeightOfChildren(ListView.java:1429)
                  at android.widget.ListView.onMeasure(ListView.java:1336)
                  at android.view.View.measure(View.java:22116)
                  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                  at android.view.View.measure(View.java:22116)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6654)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                  at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
                  at android.view.View.measure(View.java:22116)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6654)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
                  at android.view.View.measure(View.java:22116)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6654)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                  at android.view.View.measure(View.java:22116)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6654)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
                  at android.view.View.measure(View.java:22116)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6654)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                  at com.android.internal.policy.DecorView.onMeasure(DecorView.java:740)
                  at android.view.View.measure(View.java:22116)
                  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2511)
                  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1577)
                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1841)
                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1452)
                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7217)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:979)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:791)
                  at android.view.Choreographer.doFrame(Choreographer.java:726)
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:965)
                  at android.os.Handler.handleCallback(Handler.java:789)
                  at android.os.Handler.dispatchMessage(Handler.java:98)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6710)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)

0 个答案:

没有答案