简体   繁体   中英

App crashes when retrieving data after user input all data?

My code on how to retrieve data from the database. The data successfully saves in the database and suddenly my app crashes and the data can't be retrieved...

ViewAdsRef.addValueEventListener(new ValueEventListener()
    {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot)
        {
            if ((dataSnapshot.exists()))
            {
                String retrieveTuitionImage = dataSnapshot.child("adstuitionimage").getValue().toString();
                String retrieveTuitionName = dataSnapshot.child("adstuitioname").getValue().toString();
                String retrieveProviderName = dataSnapshot.child("adsprovidername").getValue().toString();

                Picasso.get().load(retrieveTuitionImage).placeholder(R.drawable.photocamera).into(ViewTuitionImage);

                ViewTuitionName.setText(retrieveTuitionName);
                ViewProviderName.setText(retrieveProviderName);
            }

            else
            {

            }
        }

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

        }
    });

Logcat shows below:-

08-26 12:17:16.345 15709-15709/com.addaj.mobilerecommendationsystem E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 08-26 12:17:17.126 15709-15709/com.addaj.mobilerecommendationsystem E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 08-26 12:17:17.127 15709-15709/com.addaj.mobilerecommendationsystem E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 08-26 12:17:40.366 15709-15709/com.addaj.mobilerecommendationsystem E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 08-26 12:18:23.902 15709-15709/com.addaj.mobilerecommendationsystem E/AndroidRuntime: FATAL EXCEPTION: main Process: com.addaj.mobilerecommendationsystem, PID: 15709 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.addaj.mobilerecommendationsystem/com.addaj.mo bilerecommendationsystem.ViewAdsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2721) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2782) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1521) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6228) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolea n)' on a null object reference at com.addaj.mobilerecommendationsystem.ViewAdsActivity.onCreate(ViewAdsActivity.java:60) at android.app.Activity.performCreate(Activity.java:6860) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2674) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2782) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1521) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6228) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)

The error log said that your called setDisplayHomeAsUpEnabled() on a object that have not been initialized (null value) inside ViewAdsActivity .

I guess may be getActionBar() return null?

See: What is a NullPointerException, and how do I fix it?

Solution is taking the current user which was just uploaded:

ViewAdsRef= FirebaseDatabase.getInstance().getReference().child("Advertisements").child(intent.getStringExtra("UserKey"));

It works.

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