我在代码中的.settext函数中收到NullPointer异常

时间:2019-05-31 10:34:50

标签: android nullpointerexception

运行我的应用程序时,它显示空指针异常,并且我的应用程序在Usename.setText函数中崩溃,这是我的代码

请帮助我,这很紧急 我正在面对这个问题,我想解决这个问题,这是我的Firebase Project图片

package com.example.socialnetwork;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import com.google.firebase.auth.FirebaseAuth;
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 com.squareup.picasso.Picasso;

import de.hdodenhof.circleimageview.CircleImageView;

public class ProfileActivity extends AppCompatActivity {
    private TextView userName, userProfname, userStatus, UserCountry, userDob, UserGender, UserRealtionshipStatus;
    private CircleImageView UserProfileImage;
    private DatabaseReference ProfileUserRef;
    private FirebaseAuth mAuth;
    private String currentUserId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        userName = (TextView) findViewById(R.id.my_user_name);
        userProfname = (TextView) findViewById(R.id.my_profle_full_name);
        userDob = (TextView) findViewById(R.id.Dob);
        userStatus = (TextView) findViewById(R.id.my_profile_status);
        UserCountry = (TextView) findViewById(R.id.my_country);
        UserGender = (TextView) findViewById(R.id.gender);
        UserRealtionshipStatus = (TextView) findViewById(R.id.RelationShipStatus);
        UserProfileImage = (CircleImageView) findViewById(R.id.my_profile_pic) ;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        mAuth = FirebaseAuth.getInstance();
        currentUserId = mAuth.getCurrentUser().getUid();
        ProfileUserRef = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserId);
        ProfileUserRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()){
                    String myProfileImage;
                    String myUserName;
                    String myProfileName;
                    String myStatus;
                    String myDob;
                    String myCountry;
                    String myGender;
                    String myRelationshipStatus;


                    myProfileImage = dataSnapshot.child("profileImage").getValue().toString();
                    myUserName = dataSnapshot.child("Username").getValue().toString();
                    myProfileName = dataSnapshot.child("fullName").getValue().toString();
                    myStatus = dataSnapshot.child("Status").getValue().toString();
                    myDob = dataSnapshot.child("dob").getValue().toString();
                    myCountry = dataSnapshot.child("country").getValue().toString();
                    myGender = dataSnapshot.child("gender").getValue().toString();
                    myRelationshipStatus = dataSnapshot.child("Relationship").getValue().toString();

                    //Picasso.get().load(myProfileImage).placeholder(R.drawable.profile).into(UserProfileImage);

                    userName.setText("@" +myUserName);
                    userProfname.setText(myProfileName);
                    userDob.setText("DOB" +myDob);
                    UserCountry.setText("Country"+myCountry);
                    UserGender.setText("Gender"+myGender);
                    userStatus.setText(myStatus);
                    UserRealtionshipStatus.setText("Relationship"+myRelationshipStatus);

                }
            }

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

            }
        });
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ProfileActivity"
    android:background="@color/colorProfile">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <de.hdodenhof.circleimageview.CircleImageView
                android:layout_width="300dp"
                android:id="@+id/my_profile_pic"
                android:layout_height="300dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="15dp"
                android:src="@drawable/profile"/>
            <TextView
                android:id="@+id/my_profle_full_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Profile name"
                android:layout_below="@+id/my_profile_pic"
                android:layout_marginTop="5dp"
                android:textAlignment="center"
                android:textAllCaps="false"
                android:textColor="@android:color/background_light"
                android:textSize="20sp"
                android:textStyle="bold"/>
            <TextView
                android:id="@+id/my_user_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="user name"
                android:layout_below="@+id/my_profle_full_name"
                android:textAlignment="center"
                android:textAllCaps="false"
                android:textColor="@android:color/background_light"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/my_profile_status"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/my_user_name"
                android:layout_marginTop="5dp"
                android:text="hey there i am using app "
                android:textAlignment="center"
                android:textAllCaps="false"
                android:textColor="@android:color/darker_gray"
                android:textSize="22sp" />

            <TextView
                android:id="@+id/my_country"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/my_profile_status"
                android:layout_marginTop="60dp"
                android:text="Country "
                android:textAlignment="center"
                android:textAllCaps="false"
                android:textColor="@color/colorAccent"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/Dob"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/my_country"
                android:layout_marginTop="2dp"
                android:text=" Date of Birth"
                android:textAlignment="center"
                android:textAllCaps="false"
                android:textColor="@color/colorAccent"
                android:textSize="18sp" />
            <TextView
                android:id="@+id/gender"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/Dob"
                android:layout_marginTop="2dp"
                android:text=" Gender"
                android:textAlignment="center"
                android:textAllCaps="false"
                android:textColor="@color/colorAccent"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/RelationShipStatus"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/gender"
                android:layout_marginTop="2dp"
                android:text=" Relationship Status"
                android:textAlignment="center"
                android:textAllCaps="false"
                android:textColor="@color/colorAccent"
                android:textSize="18sp" />
        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

它向我显示了空指针异常,如下所示: 流程:com.example.socialnetwork,PID:24974

    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.socialnetwork.ProfileActivity$1.onDataChange(ProfileActivity.java:67)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@17.0.0:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@17.0.0:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@17.0.0:55)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7058)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
2019-05-31 15:49:01.185 25489-25489/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2019-05-31 15:49:01.186 25489-25489/? E/Zygote: accessInfo : 1
2019-05-31 15:49:01.197 25489-25489/? E/android.dqagen: Not starting debugger since process cannot load the jdwp agent.
2019-05-31 15:49:01.199 4949-5117/? E/rs$DefaultThreadFactory: ===== Detect pool-thread leak =====
2019-05-31 15:49:01.201 4949-5117/? E/rs$DefaultThreadFactory: dalvik.system.VMStack.getThreadStackTrace(Native Method)
2019-05-31 15:49:01.201 4949-5117/? E/rs$DefaultThreadFactory: java.lang.Thread.getStackTrace(Thread.java:1538)
2019-05-31 15:49:01.201 4949-5117/? E/rs$DefaultThreadFactory: java.util.concurrent.Executors$DefaultThreadFactory.newThread(Executors.java:569)
2019-05-31 15:49:01.201 4949-5117/? E/rs$DefaultThreadFactory: sej.newThread(Unknown Source:9)
2019-05-31 15:49:01.201 4949-5117/? E/rs$DefaultThreadFactory: java.util.concurrent.ThreadPoolExecutor$Worker.<init>(ThreadPoolExecutor.java:636)
2019-05-31 15:49:01.201 4949-5117/? E/rs$DefaultThreadFactory: java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:950)
2019-05-31 15:49:01.201 4949-5117/? E/rs$DefaultThreadFactory: java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1393)
2019-05-31 15:49:01.201 4949-5117/? E/rs$DefaultThreadFactory: ryi.a(:com.google.android.gms@17122037@17.1.22 (100400-245988633):13)
2019-05-31 15:49:01.201 4949-5117/? E/rs$DefaultThreadFactory: ryi.execute(:com.google.android.gms@17122037@17.1.22 (100400-245988633):2)
2019-05-31 15:49:01.201 4949-5117/? E/rs$DefaultThreadFactory: rkx.a(:com.google.android.gms@17122037@17.1.22 (100400-245988633):2)
2019-05-31 15:49:01.201 4949-5117/? E/rs$DefaultThreadFactory: rhg.a(:com.google.android.gms@17122037@17.1.22 (100400-245988633):35)
2019-05-31 15:49:01.201 4949-5117/? E/rs$DefaultThreadFactory: rke.onTransact(:com.google.android.gms@17122037@17.1.22 (100400-245988633):36)
2019-05-31 15:49:01.202 4949-5117/? E/rs$DefaultThreadFactory: android.os.Binder.transact(Binder.java:675)
2019-05-31 15:49:01.202 4949-5117/? E/rs$DefaultThreadFactory: dze.onTransact(:com.google.android.gms@17122037@17.1.22 (100400-245988633):3)
2019-05-31 15:49:01.202 4949-5117/? E/rs$DefaultThreadFactory: android.os.Binder.execTransact(Binder.java:739)
2019-05-31 15:49:01.228 4259-4407/? E/InputDispatcher: channel '4e0409b com.example.socialnetwork/com.example.socialnetwork.ProfileActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-05-31 15:49:01.228 4259-4407/? E/InputDispatcher: channel '187a2d9 Toast (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-05-31 15:49:01.228 4259-4407/? E/InputDispatcher: channel '60a4015 com.example.socialnetwork/com.example.socialnetwork.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-05-31 15:49:01.295 4259-4548/? E/WindowManager: win=Window{60a4015 u0 com.example.socialnetwork/com.example.socialnetwork.MainActivity EXITING} destroySurfaces: appStopped=false win.mWindowRemovalAllowed=true win.mRemoveOnExit=true win.mViewVisibility=0 caller=com.android.server.wm.AppWindowToken.destroySurfaces:870 com.android.server.wm.AppWindowToken.destroySurfaces:851 com.android.server.wm.WindowState.onExitAnimationDone:5413 com.android.server.wm.-$$Lambda$01bPtngJg5AqEoOWfW3rWfV7MH4.accept:2 java.util.ArrayList.forEach:1262 com.android.server.wm.AppWindowToken.onAnimationFinished:2398 com.android.server.wm.AppWindowToken.setVisibility:551 
2019-05-31 15:49:01.328 5870-30827/? E/SDHMS:SDHMS_UTIL_IO: e = /sys/class/input_booster/touchkey/time (Permission denied)
2019-05-31 15:49:01.329 5870-30827/? E/SDHMS:SDHMS_UTIL_IO: e = /sys/class/input_booster/touchkey/time (Permission denied)
2019-05-31 15:49:01.330 5870-30827/? E/SDHMS:SDHMS_UTIL_IO: e = /sys/class/input_booster/touchkey/time (Permission denied)
2019-05-31 15:49:01.572 31289-31310/? E/PBSessionCacheImpl: sessionId[137612036556413490] not persisted.
2019-05-31 15:49:01.962 4509-4996/? E/ImsAdaptorImpl2: setSSACInfo : ImsAdaptorImpl.
2019-05-31 15:49:01.969 4509-4996/? E/ImsAdaptorImpl2: setSSACInfo : ImsAdaptorImpl.
2019-05-31 15:49:04.931 4259-4402/? E/MotionRecognitionService: handleMessage: event 200 value : 1
2019-05-31 15:49:12.849 5449-8846/? E/MdnsClient: Failed to send a multicast mDNS packet.
    java.io.IOException: sendto failed: ENETUNREACH (Network is unreachable)
        at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:576)
        at libcore.io.IoBridge.sendto(IoBridge.java:544)
        at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:125)
        at java.net.DatagramSocket.send(DatagramSocket.java:723)
        at agho.a(:com.google.android.gms@17122037@17.1.22 (100400-245988633):82)
        at aghr.run(Unknown Source:6)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: android.system.ErrnoException: sendto failed: ENETUNREACH (Network is unreachable)
        at libcore.io.Linux.sendtoBytes(Native Method)
        at libcore.io.Linux.sendto(Linux.java:227)
        at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:307)
        at libcore.io.IoBridge.sendto(IoBridge.java:542)
        at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:125) 
        at java.net.DatagramSocket.send(DatagramSocket.java:723) 
        at agho.a(:com.google.android.gms@17122037@17.1.22 (100400-245988633):82) 
        at aghr.run(Unknown Source:6) 
        at java.lang.Thread.run(Thread.java:764) 
2019-05-31 15:49:13.603 4509-4996/? E/ImsAdaptorImpl2: setSSACInfo : ImsAdaptorImpl.
2019-05-31 15:49:13.621 4509-4996/? E/ImsAdaptorImpl2: setSSACInfo : ImsAdaptorImpl.
2019-05-31 15:49:15.182 4259-4402/? E/MotionRecognitionService: handleMessage: event 200 value : 1
2019-05-31 15:49:23.592 4259-4663/? E/Watchdog: !@Sync 3098 [2019-05-31 15:49:23.592] FD count : 582
2019-05-31 15:49:25.423 4259-4402/? E/MotionRecognitionService: handleMessage: event 200 value : 1
2019-05-31 15:49:32.665 4259-4402/? E/MotionRecognitionService: handleMessage: event 200 value : 1
2019-05-31 15:49:32.674 4259-4402/? E/MotionRecognitionService: handleMessage: event 200 value : 2
2019-05-31 15:49:32.852 5449-8846/? E/MdnsClient: Failed to send a multicast mDNS packet.

0 个答案:

没有答案