为什么我收到此错误? [英] Why am I getting this error?

查看:82
本文介绍了为什么我收到此错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个带有Firebase数据库的Pizza Ordering App,我想用数据库中的数据填充RecyclerView。



这是数据库:< br $>




So, I have this Pizza Ordering App with a Firebase Database where I want to populate a RecyclerView with data from the database.

Here is the database:


{
  "category" : {
    "01" : {
      "image" : "https://www.carusopizza.cz/185-large_default/prosciutto.jpg",
      "name" : "Pizza Prosciuto"
    },
    "02" : {
      "image" : "http://urgentpizza.ro/blog/wp-content/uploads/2014/06/pizza-diavola-urgentpizza1.jpg",
      "name" : "Pizza Diavolo"
    },
    "03" : {
      "image" : "https://www.carusopizza.cz/388/1047.jpg",
      "name" : "Pizza Funghi"
    }
  },
  "user" : {
    "12345" : {
      "name" : "Tiganu",
      "password" : "1234"
    }
  }
}





LoadMenu()代码(Recycler View)





LoadMenu() Code (Recycler View)

private void LoadMenu(){
        Query query =  FirebaseDatabase.getInstance().getReference().child("category");
        FirebaseRecyclerOptions<Category> options = new FirebaseRecyclerOptions.Builder<Category>()
                .setQuery(query, new SnapshotParser<Category>() {
                    @NonNull
                    @Override
                    public Category parseSnapshot(@NonNull DataSnapshot snapshot) {
                        return new Category(snapshot.child("image").getValue().toString(), snapshot.child("name").getValue().toString());
                    }
                }).build();
        adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull MenuViewHolder holder, int position, @NonNull Category model) {
                holder.menuName.setText(model.getName());
                Picasso.with(getBaseContext()).load(model.getImage()).into(holder.imageView);
                final Category clickItem = model;
                holder.setItemClickListener(new ItemClickListener() {
                    @Override
                    public void onClick(View view, int position, boolean isLongClick) {
                        Toast.makeText(HomeActivity.this, "" + clickItem.getName(), Toast.LENGTH_SHORT).show();
                    }
                });
            }

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





类别类





Category class

package com.example.pizzaapp;

public class Category {
    private String image;
    private String name;
    public Category(){

    }
    public Category(String image, String name){
        this.image = image;
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public String getImage(){
        return image;
    }
    public void setName(String name){
        this.name = name;
    }
    public void setImage(String image){
        this.image = image;
    }
}







MenuViewHolder Class






MenuViewHolder Class

package com.example.pizzaapp;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MenuViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    public TextView menuName;
    public ImageView imageView;
    private ItemClickListener itemClickListener;
    public MenuViewHolder(View itemView){
        super(itemView);
        menuName = (TextView)itemView.findViewById(R.id.menu_name);
        imageView = (ImageView)itemView.findViewById(R.id.menu_image);
        itemView.setOnClickListener(this);
    }
    public void setItemClickListener(ItemClickListener itemClickListener){
        this.itemClickListener = itemClickListener;
    }
    @Override
    public void onClick(View view){
        itemClickListener.onClick(view, getAdapterPosition(), false);
    }
    public TextView getMenuName(){
        return menuName;
    }
    public ImageView getImageView(){
        return imageView;
    }
    public void setMenuName(TextView view){
        menuName = view;
    }
    public void setImageView(ImageView view){
        imageView = view;
    }
}





问题是我一直收到这个错误





The problem is I am keep getting this error

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.pizzaapp.HomeActivity$2.onBindViewHolder(HomeActivity.java:58)
        at com.example.pizzaapp.HomeActivity$2.onBindViewHolder(HomeActivity.java:55)






我不知道为什么用户root的数据被正确读取而且类别中的数据是不。我认为这是查询的问题。



第55行:






in the LoadMenu() function and I don't know why The data from the user root is read correctly and the data from category is not. I think it is a problem with the query.

Line 55:

adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(options) {





第58行:





Line 58:

holder.menuName.setText(model.getName());





还ContentHome.xml





Also ContentHome.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".HomeActivity"
    tools:showIn="@layout/app_bar_home">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical">

    </android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>





我的尝试:



我试图在互联网上搜索,似乎我的数据库返回一个空值,我不明白为什么因为我的用户数据库工作正常



What I have tried:

I've tried to search on the internet and it seems my database is returning a null value and I don't understand why because my user database is working correctly

推荐答案

2.onBindViewHolder(HomeActivity.java:58)
at com.example.pizzaapp.HomeActivity
2.onBindViewHolder(HomeActivity.java:58) at com.example.pizzaapp.HomeActivity


2 .onBindViewHolder(HomeActivity.java:55)
2.onBindViewHolder(HomeActivity.java:55)






在LoadMenu()函数中
我不喜欢知道原因用户root的数据是否正确读取而类别的数据不是。我认为这是查询的问题。



第55行:






in the LoadMenu() function and I don't know why The data from the user root is read correctly and the data from category is not. I think it is a problem with the query.

Line 55:

adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(options) {





第58行:





Line 58:

holder.menuName.setText(model.getName());





还ContentHome.xml





Also ContentHome.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".HomeActivity"
    tools:showIn="@layout/app_bar_home">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical">

    </android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>





我的尝试:



我试图在互联网上搜索,似乎我的数据库返回一个空值,我不明白为什么因为我的用户数据库工作正常



What I have tried:

I've tried to search on the internet and it seems my database is returning a null value and I don't understand why because my user database is working correctly


引用:

似乎我的数据库返回一个空值,我不明白为什么因为我的用户数据库工作正常

it seems my database is returning a null value and I don't understand why because my user database is working correctly



不要猜测,确保使用调试器并检查变量和返回值,您将看到确切的返回值为空时。

我们可以(因为我们没有数据库而进行此检查。



您的代码行为不符合您的预期,或者您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器是h是为了向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道您的代码应该做什么,它没有发现错误,只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。


Don't guess, make sure instead, use the debugger and check variables and return values, you will see exactly when there is a null return value.
We can(t do this checking because we don't have the database.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于为什么我收到此错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆