从数据库到Android应用程序检索数据时出现问题

时间:2020-10-17 07:03:59

标签: android json nullpointerexception

我正在尝试制作比赛应用。 它将从服务器接收比赛信息,并将该信息显示到android应用程序中。 但我收到此错误。我是android开发的新手。 我正在使用XAMPP和phpmyadmin。

2020-10-17 12:33:57.758 24170-24170/com.example.GameTour E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.GameTour, PID: 24170
    java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
        at com.example.GameTourMySQL.TournamentAdapter.getItemCount(TournamentAdapter.java:53)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4044)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3849)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1103)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at androidx.drawerlayout.widget.DrawerLayout.onLayout(DrawerLayout.java:1231)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:784)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3470)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2938)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
        at android.view.Choreographer.doCallbacks(Choreographer.java:796)
        at android.view.Choreographer.doFrame(Choreographer.java:731)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

这是我的代码。它显示错误在第53行。我从一些在线文章中寻求帮助。但是我无法解决问题。我做错了什么?

    package com.example.GameTourMySQL;
    import android.content.Context;
    import androidx.annotation.NonNull;
    import androidx.recyclerview.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;
    import com.bumptech.glide.Glide;
    import java.util.ArrayList;
    import java.util.List;

    public class TournamentAdapter extends RecyclerView.Adapter<TournamentAdapter.TournamentViewHolder> {


        private final Context mCtx;
        private final List<Tournaments> tournaments;

        public TournamentAdapter(Context mCtx, List<Tournaments> tournaments) {
            this.mCtx = mCtx;
            this.tournaments = tournaments;
        }

        @NonNull
        @Override
        public TournamentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(mCtx);
            View view = inflater.inflate(R.layout.item_layout, null);
            return new TournamentViewHolder(view);
        }

        @Override
        public void onBindViewHolder(TournamentViewHolder holder, int position) {
            Tournaments tour = tournaments.get(position);

            holder.title.setText(tour.getTitle());
            holder.time.setText(tour.getTime());
            holder.reward.setText(tour.getReward());
            holder.perkill.setText(tour.getPerkill());
            holder.entryfee.setText(tour.getFees());
            holder.type.setText(tour.getType());
            holder.map.setText(tour.getMap());
            holder.joincount.setText(tour.getJoin());
            holder.total.setText(tour.getTotal());
        }

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

        class TournamentViewHolder extends RecyclerView.ViewHolder {

            TextView title, time, reward, perkill, entryfee, type, map, joincount, total;
            Button roomPassButton, join;

            public TournamentViewHolder(View itemView) {
               super(itemView);

                title = itemView.findViewById(R.id.title);
                time = itemView.findViewById(R.id.time);
                reward = itemView.findViewById(R.id.reward);
                perkill = itemView.findViewById(R.id.perkill);
                entryfee = itemView.findViewById(R.id.entryfee);
                type = itemView.findViewById(R.id.type);
                map = itemView.findViewById(R.id.map);
                joincount = itemView.findViewById(R.id.joincount);
                total = itemView.findViewById(R.id.total);
                roomPassButton = itemView.findViewById(R.id.roomPassButton);
                join = itemView.findViewById(R.id.join);
            }
        }
    }

我该怎么办? 谢谢

2 个答案:

答案 0 :(得分:0)

此错误导致“ NullPointerException”。首先看“什么是NullPointerExcetion?”。手段。

如果您没有给参数赋值,并且使用了函数或参数,则操作系统会为您提供“ NullPointerException”。

在这种情况下。您没有为“比赛”参数分配值。您在下面使用了“大小”功能。

您的问题是“在从数据库向Android应用程序检索数据时出现问题”,但是没有关于数据库或网络信息的说明。 如果出现“ NullPointerException”崩溃,请以“我正在获取Nullpointer异常等”的方式打开问题。 并且请注意,这个问题以前没有被问过。

答案 1 :(得分:0)

首先,在调用适配器的构造函数的地方发布代码,即TournamentAdapter()。从堆栈跟踪中可以明显看出tournaments.size();正在抛出NullPointerException。因此,请检查要发送给适配器的第二个参数是什么。我认为该值为null,因此您得到了例外,希望这能回答您的问题!