Java 通过pojo和我的模型类';s getter()方法返回null

Java 通过pojo和我的模型类';s getter()方法返回null,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我正在使用应用程序,但当我尝试访问firebase数据时,POJO的getter方法返回null。我无法访问RecyclerView中的数据和设置 以下是我所做的: 我的Firebase结构: 我的应用程序调试模式: 我的书本课 public class Book { private String bookId; private String bookImage; private String bookName; private String writerName; private Stri

我正在使用应用程序,但当我尝试访问
firebase
数据时,
POJO
的getter方法返回
null
。我无法访问
RecyclerView中的数据和设置

以下是我所做的:

我的Firebase结构:

我的应用程序调试模式:

我的书本课

public class Book {
private String bookId;
private String bookImage;
private String bookName;
private String writerName;
private String publisherName;
private String shortDesc;


public Book() {
}

public Book(String bookImage, String bookName,
            String writerName, String publisherName,String shortDesc) {
    this.bookImage = bookImage;
    this.bookName = bookName;
    this.writerName = writerName;
    this.publisherName = publisherName;
    this.shortDesc = shortDesc;
}

public Book(String bookId, String bookImage, String bookName,
            String writerName, String publisherName, String shortDesc) {
    this.bookId = bookId;
    this.bookImage = bookImage;
    this.bookName = bookName;
    this.writerName = writerName;
    this.publisherName = publisherName;
    this.shortDesc = shortDesc;
}

public String getBookId() {
    return bookId;
}

public String getBookImage() {
    return bookImage;
}

public String getBookName() {
    return bookName;
}

public String getWriterName() {
    return writerName;
}

public String getPublisherName() {
    return publisherName;
}

public String getShortDesc() {
    return shortDesc;
}

public void setBookId(String bookId) {
    this.bookId = bookId;
}
我的书单片段,显示我的书单

public class BookListFragment extends Fragment {

private Button button1,button2,button3;
private RecyclerView bookListRv;
private ArrayList<Book> bookList;
private BookAdapter bookAdapter;
private String adminId;
private FirebaseAuth firebaseAuth;
private DatabaseReference databaseReference;



public BookListFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment_book_list, container, false);

    init(view);

    adminId = firebaseAuth.getUid();
     Book book = new Book();
    String bookid =book.getBookId();

    configRV();

    getBooks();



    return view;
}



private void init(View view) {
    bookListRv = view.findViewById(R.id.bookRVId);
    firebaseAuth = firebaseAuth.getInstance();
    databaseReference = FirebaseDatabase.getInstance().getReference();
    bookList = new ArrayList<>();
    bookAdapter = new BookAdapter(bookList, getContext());



}

private void configRV() {

    bookListRv.setLayoutManager(new LinearLayoutManager(getContext()));
    bookListRv.setAdapter(bookAdapter);
}

private void getBooks() {

    DatabaseReference showBookRef = databaseReference.child("Books").child(adminId);

    showBookRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                for (DataSnapshot data:dataSnapshot.getChildren()){
                    Book book = data.getValue(Book.class);
                    bookList.add(book);
                    bookAdapter.notifyDataSetChanged();
                }
            }
        }

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

        }
    });
}
公共类BookListFragment扩展了片段{
私人按钮按钮1、按钮2、按钮3;
私人回收站查看书目;
私人ArrayList书目;
私人图书适配器;
私有字符串adminId;
私有FirebaseAuth FirebaseAuth;
私有数据库参考数据库参考;
公共BookListFragment(){
//必需的空公共构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图=充气机。充气(R.layout.fragment\u book\u list,container,false);
初始(视图);
adminId=firebaseAuth.getUid();
书=新书();
字符串bookid=book.getBookId();
configRV();
getBooks();
返回视图;
}
私有void init(视图){
bookListRv=view.findViewById(R.id.bookRVId);
firebaseAuth=firebaseAuth.getInstance();
databaseReference=FirebaseDatabase.getInstance().getReference();
bookList=newarraylist();
bookAdapter=newbookadapter(bookList,getContext());
}
私有void configRV(){
setLayoutManager(新的LinearLayoutManager(getContext());
bookListRv.setAdapter(bookapter);
}
私人书籍{
DatabaseReference showBookRef=DatabaseReference.child(“图书”).child(adminId);
showBookRef.addValueEventListener(新的ValueEventListener(){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
if(dataSnapshot.exists()){
对于(DataSnapshot数据:DataSnapshot.getChildren()){
Book Book=data.getValue(Book.class);
图书目录。添加(图书);
bookAdapter.notifyDataSetChanged();
}
}
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){
}
});
}
}

我的AddBook活动

公共类AddBookActivity扩展了AppCompatActivity{

private Button addBookBtn;
private ImageView bookIv;
private EditText bookNameEt,writerNameEt,publisherNameEt,shortDescEt;
private Uri imgUrl = null;
private FirebaseAuth firebaseAuth;
private DatabaseReference databaseReference;
private StorageReference imgReference;
private String adminId,imgUri="";
private FirebaseUser mFirebaseUser;

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

    init();

    if(mFirebaseUser!=null){
        adminId = mFirebaseUser.getUid();
    }


    bookIv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent,1);
        }
    });

    addBookBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addBookData();

        }
    });

}




private void init() {
    firebaseAuth = FirebaseAuth.getInstance();
    mFirebaseUser = firebaseAuth.getCurrentUser();
    imgReference = FirebaseStorage.getInstance().getReference();
    databaseReference = FirebaseDatabase.getInstance().getReference();
    addBookBtn = findViewById(R.id.addBookBtnId);
    bookIv = findViewById(R.id.addBookIvId);
    bookNameEt = findViewById(R.id.addBookNameEtId);
    writerNameEt = findViewById(R.id.addBookWriterNameEtId);
    publisherNameEt = findViewById(R.id.addBookPublisherNameEtId);
    shortDescEt = findViewById(R.id.addBookDescripEtId);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode==1 && resultCode==RESULT_OK){
        if(data!=null){
             imgUrl = data.getData();
            bookIv.setImageURI(imgUrl);


        }
    }
}



private void addBookData() {

    if (imgUrl!=null) {
        final StorageReference filePath = imgReference.child("Book_images").child(String.valueOf(System.currentTimeMillis()));
        filePath.putFile(imgUrl).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                if (task.isSuccessful()) {
                    filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            imgUri=uri.toString();
                        }
                    });
                }
            }
        });
    }

    String bookName = bookNameEt.getText().toString();
    String writerName = writerNameEt.getText().toString();
    String publisherName = publisherNameEt.getText().toString();
    String bookDesc = shortDescEt.getText().toString();
    String bookImg = imgUri;

    if(TextUtils.isEmpty(bookName) && TextUtils.isEmpty(writerName)
            && TextUtils.isEmpty(publisherName) && TextUtils.isEmpty(bookDesc) && TextUtils.isEmpty(bookImg)){
        Toast.makeText(this,"Please Fill The Blank Field",Toast.LENGTH_LONG).show();
    }
    else if(!TextUtils.isEmpty(bookName) && !TextUtils.isEmpty(writerName)
            && !TextUtils.isEmpty(publisherName) && !TextUtils.isEmpty(bookDesc) && !TextUtils.isEmpty(bookImg)){

        saveBook(bookImg,bookName,writerName,publisherName,bookDesc);
    }
}

private void saveBook(String url, String bookName, String writerName, String publisherName, String bookDesc) {

    DatabaseReference bookRef = databaseReference.child("Books").child(adminId);
    String bookId = bookRef.push().getKey();
    if(bookId!=null){
        Book book = new Book(bookId,url,bookName,writerName,publisherName,bookDesc);
        bookRef.child(bookId).child("BookInfo").setValue(book).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {

            }
        });
    }



}
专用按钮addBookBtn;
私人影像查看第四册;
private EditText BookName et、WriterName、PublisherName、ShortDeset;
私有Uri imgUrl=null;
私有FirebaseAuth FirebaseAuth;
私有数据库参考数据库参考;
私有存储引用;
私有字符串adminId,imgUri=“”;
私有FirebaseUser mFirebaseUser;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u add\u book);
init();
if(mFirebaseUser!=null){
adminId=mFirebaseUser.getUid();
}
bookIv.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
意向意向=新意向(意向.行动\u获取\u内容);
intent.setType(“image/*”);
startActivityForResult(意向,1);
}
});
addBookBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
addBookData();
}
});
}
私有void init(){
firebaseAuth=firebaseAuth.getInstance();
mFirebaseUser=firebaseAuth.getCurrentUser();
imgReference=FirebaseStorage.getInstance().getReference();
databaseReference=FirebaseDatabase.getInstance().getReference();
addBookBtn=findviewbyd(R.id.addBookBtnId);
bookIv=findViewById(R.id.addBookIvId);
bookNameEt=findviewbyd(R.id.addbooknameeti);
WriterName=findViewById(R.id.addBookWriterName);
PublisherName=findViewById(R.id.AddBookPublisherName);
shortDescEt=findViewById(R.id.addBookDescriptId);
}
@凌驾
受保护的void onActivityResult(int-requestCode、int-resultCode、@Nullable-Intent-data){
super.onActivityResult(请求代码、结果代码、数据);
if(requestCode==1&&resultCode==RESULT\u确定){
如果(数据!=null){
imgUrl=data.getData();
bookIv.setImageURI(imgUrl);
}
}
}
私有void addBookData(){
如果(imgUrl!=null){
final-StorageReference filePath=imgreeference.child(“Book_images”).child(String.valueOf(System.currentTimeMillis());
filePath.putFile(imgUrl).addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
filePath.getDownloadUrl().addOnSuccessListener(新OnSuccessListener()){
@凌驾
成功时的公共无效(Uri){
imgUri=uri.toString();
}
});
}
}
});
}
字符串bookName=booknamet.getText().toString();
String writerName=writerName.getText().toString();
字符串publisherName=publisherName.getText().toString();
字符串bookDesc=shortDescEt.getText().toString();
字符串bookImg=imgUri;
if(TextUtils.isEmpty(书名)和&TextUtils.isEmpty(writerName)
&&TextUtils.isEmpty(publisherName)和&TextUtils.isEmpty(bookDesc)和&TextUtils.isEmpty(bookImg)){
MakEXT(这是“请填写空白字段”,toAST.LangthyLon)。
}
如果(!TextUtils.isEmpty(书名)和&!TextUtils.isEmpty(writerName),则为else
&&!TextUtils.isEmpty(publisherName)&&!TextUtils.isEmpty(bookDesc)&&!TextUtils.isEmpty(bookImg)){
存折(bookImg、bookName、writerName、publisherName、bookDesc);
}
}
私人vo
private List<Book> books;
private Context context;

public BookAdapter(List<Book> books, Context context) {
    this.books = books;
    this.context = context;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_book_layout,parent,false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    final Book currentBook = books.get(position);
   // holder.bookIv.setIm(currentBook.getBookImage());
    holder.bookNameTv.setText(currentBook.getBookName());
    holder.writerNameTv.setText(currentBook.getWriterName());
    holder.publisherNameTv.setText(currentBook.getPublisherName());
    holder.shortDescTv.setText(currentBook.getShortDesc());
}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    private TextView bookNameTv,writerNameTv,publisherNameTv,shortDescTv;
    private ImageView bookIv;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);

         bookIv = itemView.findViewById(R.id.bookListIvId);
         bookNameTv = itemView.findViewById(R.id.bookListNameTvId);
         writerNameTv = itemView.findViewById(R.id.bookListWriterNameTvId);
         publisherNameTv = itemView.findViewById(R.id.bookListPubNameTvId);
         shortDescTv = itemView.findViewById(R.id.bookListShortDescTvId);
    }
}
Query query = FirebaseDatabase.getInstance()
    .getReference()
    .child("yourRootId/");

FirebaseRecyclerOptions<Book> options = FirebaseRecyclerOptions.Builder<Book>()
                    .setQuery(query, Book.class)
                    .build();

FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Book, BookHolder>(options) {
@Override
public BookHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // Create a new instance of the ViewHolder, in this case we are using a custom
    // layout called R.layout.message for each item
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.yourlayout, parent, false);

    return new BookHolder(view);
}

    @Override
    protected void onBindViewHolder(BookHolder holder, int position, Book model) {
    // assign all data in here
    }
};
public class BookHolder extends RecyclerView.ViewHolder {
    public LinearLayout root;
    public TextView abc;
    public TextView xyz;

    public ViewHolder(View itemView) {
        super(itemView);
        abc= itemView.findViewById(R.id.list_title);
        xyz= itemView.findViewById(R.id.list_desc);
    }
}
showBookRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for(DataSnapshot data: dataSnapshot.getChildren()) {
            Book book = data.child("BookInfo").getValue(Book.class);
            bookList.add(book);
        }

        bookAdapter.notifyDataSetChanged();
    }

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

    }
});