“ java.lang.NullPointerException”错误(错误详细信息,在本文中)我该如何解决?

时间:2019-07-25 08:22:55

标签: java android firebase firebase-realtime-database firebase-authentication

尝试在Firebase实时数据库中注册用户时收到1-2个代码错误。更正它们后,我收到了这样的错误。我在这里做了一些研究,但没有尝试。请帮助我。

    这是我收到的错误:

  

E / Android运行时:致命异常:主要           流程:com.example.yagoozonfirebasejava,PID:6130           java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.widget.TextView.setText(java.lang.CharSequence)”               在com.example.yagoozonfirebasejava.ProfilePageAdapter.onBindViewHolder(ProfilePageAdapter.java:60)               在com.example.yagoozonfirebasejava.ProfilePageAdapter.onBindViewHolder(ProfilePageAdapter.java:16)               在android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:6781)               在android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:6823)               在android.support.v7.widget.RecyclerView $ Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)               在android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)               在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5858)               在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5854)               在android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2230)               在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)               在android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)               在android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)               在android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)               在android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)               在android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194)               在android.view.View.layout(View.java:20672)               在android.view.ViewGroup.layout(ViewGroup.java:6194)               在android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1915)               在android.view.View.layout(View.java:20672)               在android.view.ViewGroup.layout(ViewGroup.java:6194)               在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)               在android.widget.FrameLayout.onLayout(FrameLayout.java:261)               在android.view.View.layout(View.java:20672)               在android.view.ViewGroup.layout(ViewGroup.java:6194)               在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)               在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)               在android.widget.LinearLayout.onLayout(LinearLayout.java:1565)               在android.view.View.layout(View.java:20672)               在android.view.ViewGroup.layout(ViewGroup.java:6194)               在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)               在android.widget.FrameLayout.onLayout(FrameLayout.java:261)               在android.view.View.layout(View.java:20672)               在android.view.ViewGroup.layout(ViewGroup.java:6194)               在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)               在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)               在android.widget.LinearLayout.onLayout(LinearLayout.java:1565)               在android.view.View.layout(View.java:20672)               在android.view.ViewGroup.layout(ViewGroup.java:6194)               在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)               在android.widget.FrameLayout.onLayout(FrameLayout.java:261)               在com.android.internal.policy.DecorView.onLayout(DecorView.java:753)               在android.view.View.layout(View.java:20672)               在android.view.ViewGroup.layout(ViewGroup.java:6194)               在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2792)               在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2319)               在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)               在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:7183)               在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:949)               在android.view.Choreographer.doCallbacks(Choreographer.java:761)               在android.view.Choreographer.doFrame(Choreographer.java:696)               在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:935)               在android.os.Handler.handleCallback(Handler.java:873)               在android.os.Handler.dispatchMessage(Handler.java:99)       E / AndroidRuntime:位于android.os.Looper.loop(Looper.java:193)               在android.app.ActivityThread.main(ActivityThread.java:6669)               在java.lang.reflect.Method.invoke(本机方法)               在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

个人资料页面适配器代码:

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.firebase.database.DatabaseReference;

import java.util.List;

public class ProfilePageAdapter extends RecyclerView.Adapter<ProfilePageAdapter.CardDesingHolder>{

    private Context mContext;
    private List<Users> usersList;
    private DatabaseReference myRef;

    public ProfilePageAdapter(Context mContext, List<Users> kisilerListe, DatabaseReference myRef) {
        this.mContext = mContext;
        this.usersList = kisilerListe;
        this.myRef = myRef;
    }

    public class CardDesingHolder extends RecyclerView.ViewHolder {

        private TextView user_RealNameProfileAdapter;
        private TextView user_surnameProfileAdapter;
        private TextView user_emailProfileAdapter;
        private TextView user_nameProfileAdapter;
        private ImageView user_ImageViewProfileAdapter;

        public CardDesingHolder(@NonNull View itemView) {
            super(itemView);

            user_RealNameProfileAdapter = itemView.findViewById(R.id.userRealName_userCard);
            user_surnameProfileAdapter = itemView.findViewById(R.id.usersurname_userCard);
            user_emailProfileAdapter = itemView.findViewById(R.id.userEmail_userCard);
            user_ImageViewProfileAdapter = itemView.findViewById(R.id.userProfileImage_userCard);
        }
    }

    @NonNull
    @Override
    public CardDesingHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.user_card, viewGroup, false);
        return new CardDesingHolder(view);
    }

    @Override
    public void onBindViewHolder(final CardDesingHolder cardDesingHolder, int position) {
        final Users user = usersList.get(position);

        cardDesingHolder.user_emailProfileAdapter.setText("Mail : " + user.getUser_email());
        cardDesingHolder.user_surnameProfileAdapter.setText("Surname : " + user.getUser_surname());
        cardDesingHolder.user_RealNameProfileAdapter.setText("Name : " + user.getUser_name());
        cardDesingHolder.user_nameProfileAdapter.setText("Username : " + user.getUser_username());


    }

    @Override
    public int getItemCount() {
        return usersList.size();
    }
}
```


SignUp Page Code's : 
```java
package com.example.yagoozonfirebasejava;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ServerValue;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;

import java.io.IOException;
import java.util.UUID;

public class signupPage extends AppCompatActivity {
private Button btn_signupSignPage;
private EditText emailText;
private EditText passwordText;
private EditText user_surname;
private EditText user_username;
private EditText user_name;


// Database and Storage
private FirebaseAuth mAuth;
FirebaseDatabase firebaseDatabase;
DatabaseReference myRef;


private StorageReference mStorageRef;

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

        user_username = findViewById(R.id.username_signPage);
        user_surname = findViewById(R.id.surname_SignUpPage);
        user_name = findViewById(R.id.name_signPage);
        btn_signupSignPage = findViewById(R.id.btn_signSignupPage);
        emailText = findViewById(R.id.mail_signupPage);
        passwordText = findViewById(R.id.pass_signupPage);

        // Database and Storage
        // Veritabanı ve Storage
        mAuth = FirebaseAuth.getInstance();
        firebaseDatabase = FirebaseDatabase.getInstance();
        myRef = firebaseDatabase.getReference("Users");
        mStorageRef = FirebaseStorage.getInstance().getReference();


    }

    // Sign Up Method
    // Kullanıcı Kayıt etme metodu
   public void signUp(View view) {
       mAuth.createUserWithEmailAndPassword(emailText.getText().toString(), passwordText.getText().toString())
               .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                   @Override
                   public void onComplete(@NonNull Task<AuthResult> task) {
                       if (task.isSuccessful()) /* Kullanıcı girişi başarılı ise bu çalışacak */ {
                           Toast.makeText(signupPage.this, "User Created", Toast.LENGTH_SHORT).show();
                           Intent homePage = new Intent(signupPage.this, ProfilePage.class);
                           startActivity(homePage);
                           finish();

                       } else /* Kullanıcı girişi başarısız ise bu çalışacak */ {
                           Intent signBack = new Intent(signupPage.this, signupPage.class);
                           startActivity(signBack);
                           finish();
                       }

                   }
               }).addOnFailureListener(this, new OnFailureListener() {
           @Override
           public void onFailure(@NonNull Exception e) {
               Toast.makeText(signupPage.this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
           }
       });

       String userName = user_name.getText().toString();
       String userSurname = user_surname.getText().toString();
       String username = user_username.getText().toString();
       String user_email = emailText.getText().toString();
       String key = myRef.push().getKey();


       Users user = new Users(key,username,userSurname,userName,user_email);

       myRef.push().setValue(user);


/*
       // Profile Image Method
       // Profil Resmi Seçme Metodu
       public void selectImage (View view){
           if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
               ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
           } else {
               Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
               startActivityForResult(gallery, 2);
           }
       }


    // Permission Control
    // İzin Kontrolü
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(gallery, 2);
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    // Profil Resmi Seçtirme
    @Override
    protected void onActivityResult(int resultCode, @Nullable Intent data) {
        if(requestCode == 2 && resultCode == RESULT_OK &&  data != null) {
            selectedImage = data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
                profilResmi.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
*/
   }

}
```


Users Class Code's : 
```java
package com.example.yagoozonfirebasejava;

public class Users {
    private String user_id;
    private String user_name;
    private String user_username;
    private String user_surname;
    private String user_email;

    // Empty Constructor
    public Users() {
    }

    // Constructor
    public Users(String user_id, String user_name, String user_username, String user_surname, String user_email ) {
        this.user_id = user_id;
        this.user_name = user_name;
        this.user_username = user_username;
        this.user_surname = user_surname;
        this.user_email = user_email;
    }

    // Getter And Setter's

    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getUser_username() {
        return user_username;
    }

    public void setUser_username(String user_username) {
        this.user_username = user_username;
    }

    public String getUser_surname() {
        return user_surname;
    }

    public void setUser_surname(String user_surname) {
        this.user_surname = user_surname;
    }

    public String getUser_email() {
        return user_email;
    }

    public void setUser_email(String user_email) {
        this.user_email = user_email;
    }

}
```

user_card XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <TextView
            android:id="@+id/userRealName_userCard"
            android:layout_marginTop="130dp"
            android:textAlignment="center"
            android:textSize="25dp"
            android:layout_width="match_parent"
            android:layout_height="41dp"
            android:text="Yılmaz Yağız" />

        <TextView
            android:id="@+id/usersurname_userCard"
            android:textAlignment="center"
            android:layout_marginTop="25dp"
            android:textSize="30dp"
            android:layout_below="@id/userRealName_userCard"
            android:layout_width="match_parent"
            android:layout_height="41dp"
            android:text="Dokumacı" />

        <ImageView
            android:id="@+id/userProfileImage_userCard"
            android:layout_marginTop="25dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/usersurname_userCard"
            tools:srcCompat="@tools:sample/avatars" />

        <TextView
            android:id="@+id/userEmail_userCard"
            android:layout_marginTop="25dp"
            android:textAlignment="center"
            android:textSize="25dp"
            android:layout_below="@id/userProfileImage_userCard"
            android:layout_width="match_parent"
            android:layout_height="41dp"
            android:text="ddismnaklda@gmail.com" />

        <TextView
            android:id="@+id/username_userCard"
            android:layout_marginTop="25dp"
            android:textAlignment="center"
            android:textSize="25dp"
            android:layout_below="@id/userEmail_userCard"
            android:layout_width="match_parent"
            android:layout_height="41dp"
            android:text="yagoo" />


    </RelativeLayout>


</LinearLayout>

2 个答案:

答案 0 :(得分:0)

在onBindViewHolder内部

cardDesingHolder.user_emailProfileAdapter
cardDesingHolder.user_surnameProfileAdapter        
cardDesingHolder.user_RealNameProfileAdapter
cardDesingHolder.user_nameProfileAdapter

任何一个textview为空

答案 1 :(得分:0)

尝试将user_nameProfileAdapter = itemView.findViewById(R.id.userName_userCard);添加到您的CardDesingHolder构造函数中