Ich versuche, ein Tutorial für RecyclerView zu folgen. Aber wenn ich beenden und versuchen, meinen Code zu testen ich diese Nachricht erhalten:Problem mit Recycler View
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)
Ich bin ein begginer und ich verstehe nicht, diese Nachricht. Es scheint, dass ich auf diesem Code einen Fehler mit meiner setHasFixedSize() Funktion
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);
}
}
können Sie mir bitte helfen zu verstehen, was los ist. my_recycler_view ist meine Ansicht, die diese enthalten:
<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" />
und ich habe eine andere Ansicht, die Linien auf meiner Liste beschreiben. Und schließlich habe ich eine Klasse namens RecyclerViewAdapter.
Dies kann kaum die Quelle seines Absturzes sein: Es würde nicht kompilieren oder laufen, wenn es ein semantisches Problem in seinem XML gibt. – Empty2k12
@ Empty2k12 versuchen Sie es selbst; es läuft perfekt. 'android.id' ist einfach ein nicht namespaced Attribut, und da es einen Wert hat, der mit' @ + id' beginnt, wird die 'my_recycler_view' ID einfach erzeugt. –
danke, das war es. Ich habe das Thema meiner Aktivität durchsucht, ich habe nie erwartet, dass es meiner Ansicht nach war. Danke – Mohamed