java 使用回收器在 android Studio 中获取 NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28119761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get NullPointerException in android Studio with recycler
提问by Shohom Shahd
I'm new here. I followed a tutorial to create a card list using RecyclerView. The app crashes at launch and the log says:
我是新来的。我按照教程使用 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)
Pieces of code:
代码片段:
//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());
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.
我从本教程中选择了代码。2 个数组“matches”包含有关板球比赛的信息,它们显示为卡片。当他们被解雇时,他们填充第二个数组“removedItems”。第二个数组用于将删除的匹配信息添加回第一个数组。
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?
当我删除该行时,第 80 行和第 81 行显示相同的错误。错误仍然存在,但删除第 81 行后,错误日志中未显示特定行号。如果有帮助:填充了数组匹配 我对教程的代码做了一些调整,但我无法查明问题。布尔值究竟如何为空,或者错误是什么意思?
Thanks in advance. I can post my entire code if needed.
提前致谢。如果需要,我可以发布我的整个代码。
EDIT: more script:
编辑:更多脚本:
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>
回答by Simas
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
findViewById
returns null
here because you haven't yet called setContentView
. All of that should be done in onCreate
.
findViewById
返回null
这里是因为您还没有调用setContentView
. 所有这些都应该在onCreate
.
Edit:
编辑:
There's no RecylerView
with an id my_recycler_view
in your layout.
您的布局中没有RecylerView
带有 idmy_recycler_view
的元素。
Your tutorial told you to add it:
您的教程告诉您添加它:
I'm not sure how you've missed it.
我不知道你是怎么错过的。