Reputation: 85
i'm trying to follow a tutorial for RecyclerView. But when i finish and try to test my code i got this message:
09-18 16:01:03.486 2521-2521/com.example.gon.recycleview E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gon.recycleview, PID: 2521
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gon.recycleview/com.example.gon.recycleview.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
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.example.gon.recycleview.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I am a begginer and i don't understand this message. It seems that i have an error on this code with my setHasFixedSize() function
public class MainActivity extends Activity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private String[] myDataset = {"Lundi", "Mardi", "Mercredi", "Vendredi", "Samedi", "Dimanche"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycler_view);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager=new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter=new RecyclerViewAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter);
}
}
can you help me please to understand what's wrong. my_recycler_view is my view which contain this :
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android.id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
and I have an other view which describe lines of my list. And finally I have a class named RecyclerViewAdapter.
Upvotes: 0
Views: 54
Reputation: 54194
In your XML layout, you have this line:
android.id="@+id/my_recycler_view"
change it to this instead:
android:id="@+id/my_recycler_view"
Because you've written android.id
instead of android:id
, the view isn't getting an id. Since it doesn't have an id, findViewById()
can't find it, so it returns null
(leading to your error).
Upvotes: 3