2016-07-29 8 views
0

Die Haupttätigkeit:RecyclerView ist null

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 

     pAdapter = new ProductAdapter(productList); 
     RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
     recyclerView.setLayoutManager(mLayoutManager); 
    } 

Ich erhalte diese Fehlermeldung:

java.lang.RuntimeException: Unable to start activity ComponentInfo{in.co.thehawker.thehawker/in.co.thehawker.thehawker.MainActivity}: java.lang.NullPointerException 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139) 
                       at android.app.ActivityThread.access$700(ActivityThread.java:143) 
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241) 
                       at android.os.Handler.dispatchMessage(Handler.java:99) 
                       at android.os.Looper.loop(Looper.java:137) 
                       at android.app.ActivityThread.main(ActivityThread.java:4960) 
                       at java.lang.reflect.Method.invokeNative(Native Method) 
                       at java.lang.reflect.Method.invoke(Method.java:511) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
                       at dalvik.system.NativeStart.main(Native Method) 
                      Caused by: java.lang.NullPointerException 
                       at in.co.thehawker.thehawker.MainActivity.onCreate(MainActivity.java:36) 

content_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:showIn="@layout/activity_main" 
    tools:context=".MainActivity"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scrollbars="vertical" /> 

</RelativeLayout> 

ich gereinigt und baute das Projekt, aber Problem bleibt das Gleiche. Gibt es ein Problem mit dem activity_main-Layout, da ich die recycler_view in content_main.xml definiert habe? Bitte helfen !!!

Edited: activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:orientation="vertical"> 

     <include 
      android:id="@+id/toolbar" 
      layout="@layout/toolbar" /> 
    </LinearLayout> 


</RelativeLayout> 
+0

Post activity_main.xml – mhenryk

+1

content_main.xml nicht in activity_main.xml inlcuded wird. Daher kann die Recyclerview nicht initialisiert werden. – sJy

Antwort

1
`RecyclerView` is not in `activity_main.xml` so `findViewById(R.id.recycler_view)` can not find it. Inflate `content_main.xml` as `view` and try like this `view.findViewById(R.id.recycler_view)` 
0
Option1: Please check productList, did it init? 
Option2: Check ProductAdapter. 
Verwandte Themen