使用回收器在Android Studio中获取NullPointerException [英] Get NullPointerException in android Studio with recycler

查看:105
本文介绍了使用回收器在Android Studio中获取NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的.我遵循了一个使用RecyclerView创建卡片清单的教程.该应用在启动时崩溃,日志显示:

I'm new here. I followed a tutorial to create a card list using RecyclerView. The app crashes at launch and the log says:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wc.gap.worldcupfixture/com.wc.gap.worldcupfixture.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
 at com.wc.gap.worldcupfixture.MainActivity.onCreate(MainActivity.java:78)

代码段:

//Variables
private static RecyclerView.LayoutManager mLayoutManager;
private static RecyclerView mRecyclerView;
private static RecyclerView.Adapter mAdapter;
private static ArrayList < Integer > removedItems;
private static ArrayList < MatchData > matches;
static View.OnClickListener myOnClickListener;
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

//error here:
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

//Line 78:
mRecyclerView.setHasFixedSize(true);

mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());

我从此教程. 比赛"这2个数组包含有关板球比赛的信息,并显示为纸牌.当关闭它们时,它们将填充第二个数组"removedItems".第二个数组用于将删除的匹配信息添加回第一个数组.

I picked the code from THIS tutorial. The 2 arrays, 'matches' contains info about cricket matches and they are displayed as cards. When they are dismissed, they fill the 2nd array 'removedItems'. And the second array is used to add the removed match info back to the first array.

当我删除该行时,在第80行显示相同的错误,然后在第81行显示.该错误仍然存​​在,但是删除第81行后,错误日志中未显示特定的行号. 如果有帮助:填充数组匹配项 我对本教程的代码进行了一些调整,但我无法指出问题所在.布尔值究竟怎么可以为null,或者错误是什么意思?

When I removed the line, the same error was shown on line 80, then 81. The error persisted, but after deleting line 81 a specific line number was not shown in the error log. If it helps: the array matches is populated I have made a few adjustments to the tutorial's codes, but i cant pinpoint the problem. Exactly how can a boolean be null, or what does the error mean?

先谢谢了.如果需要,我可以发布我的整个代码.

Thanks in advance. I can post my entire code if needed.

更多脚本:

private static RecyclerView.LayoutManager mLayoutManager;
    private static RecyclerView mRecyclerView;
    private static RecyclerView.Adapter mAdapter;
    private static ArrayList<Integer> removedItems;
    private static ArrayList<MatchData> matches;
    static View.OnClickListener myOnClickListener;


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

        myOnClickListener = new MyOnClickListener(this);
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        matches = new ArrayList<>();
        for (int i = 0; i < MyData.matchArray.length; i++){
            matches.add(new MatchData(
                    MyData.matchArray[i],
                    MyData.timingArray[i],
                    MyData.venueArray[i],
                    MyData.id_[i]
            ));
        }

        removedItems = new ArrayList<>();

        mAdapter = new MyAdapter(matches);
        mRecyclerView.setAdapter(mAdapter);

        //other things after
    }

Activity_main.

Activity_main.

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".MainActivity">


    <FrameLayout android:id="@+id/container" android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="com.wc.gap.worldcupfixture.NavigationDrawerFragment"
        tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

推荐答案

mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

findViewById在这里返回null,因为您尚未调用setContentView.所有这些都应在onCreate中完成.

findViewById returns null here because you haven't yet called setContentView. All of that should be done in onCreate.

布局中没有ID为my_recycler_viewRecylerView.

There's no RecylerView with an id my_recycler_view in your layout.

您的教程告诉您添加它:

Your tutorial told you to add it:

我不确定您是怎么想念它的.

I'm not sure how you've missed it.

这篇关于使用回收器在Android Studio中获取NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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