使用回收器在 android Studio 中获取 NullPointerException
Get NullPointerException in android Studio with recycler
我是新来的。我按照教程使用 RecyclerView 创建了一个卡片列表。该应用程序在启动时崩溃,日志显示:
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());
我从 THIS tutorial 中选择了代码。 2 个数组 'matches' 包含有关板球比赛的信息,它们显示为卡片。当他们被解雇时,他们填充第二个数组 'removedItems'。第二个数组用于将删除的匹配信息添加回第一个数组。
当我删除该行时,第 80 行和第 81 行显示了相同的错误。错误仍然存在,但删除第 81 行后,错误日志中未显示特定的行号。
如果有帮助:数组匹配已填充
我对教程的代码做了一些调整,但我无法查明问题所在。布尔值究竟如何为空,或者错误是什么意思?
提前致谢。如果需要,我可以 post 我的整个代码。
编辑:
更多脚本:
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.
<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
returns null
因为你还没有调用 setContentView
。所有这些都应该在 onCreate
.
中完成
编辑:
您的布局中没有 ID 为 my_recycler_view
的 RecylerView
。
您的教程告诉您添加它:
我不确定你是怎么错过的。
我是新来的。我按照教程使用 RecyclerView 创建了一个卡片列表。该应用程序在启动时崩溃,日志显示:
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());
我从 THIS tutorial 中选择了代码。 2 个数组 'matches' 包含有关板球比赛的信息,它们显示为卡片。当他们被解雇时,他们填充第二个数组 'removedItems'。第二个数组用于将删除的匹配信息添加回第一个数组。
当我删除该行时,第 80 行和第 81 行显示了相同的错误。错误仍然存在,但删除第 81 行后,错误日志中未显示特定的行号。 如果有帮助:数组匹配已填充 我对教程的代码做了一些调整,但我无法查明问题所在。布尔值究竟如何为空,或者错误是什么意思?
提前致谢。如果需要,我可以 post 我的整个代码。
编辑: 更多脚本:
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.
<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
returns null
因为你还没有调用 setContentView
。所有这些都应该在 onCreate
.
编辑:
您的布局中没有 ID 为 my_recycler_view
的 RecylerView
。
您的教程告诉您添加它:
我不确定你是怎么错过的。