2016-04-09 59 views
0

它以前一直在工作,但突然它沒有,即使我甚至沒有更改代碼......我在我的RecyclerView實例上得到空引用,作爲異常告訴我... 這是我的Java代碼:Android RecyclerView爲空

RecyclerView recyclerView; 
    RecyclerView.Adapter rvadapter; 
    RecyclerView.LayoutManager rvLayoutManager; 

    static ArrayList<String> itemTexte; 

    File dir; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_recycler_view); 
     dir = new File(Environment.getExternalStorageDirectory(), "Notizen"); 
     if (!dir.exists()) { 
      try { 
       dir.mkdirs(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 

     itemTexte = new ArrayList<>(); 

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

     rvLayoutManager = new LinearLayoutManager(RecyclerViewActivity.this); 
     recyclerView.setLayoutManager(rvLayoutManager); 

     rvadapter = new RvAdapterKlasse(RecyclerViewActivity.this); 
     recyclerView.setAdapter(rvadapter); 

     notizenSetup(); 
    } 

的錯誤是在recyclerView.setLayoutManager(rvLayoutManager); 這裏是我的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" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="at.wurz.david.recyclerviewtemplate.RecyclerViewActivity"> 

    <android.support.v7.widget.RecyclerView 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:id="@+id/recyclerview" 
    /> 

</RelativeLayout> 

錯誤:

04-09 10:35:23.131 15155-15155/at.wurz.david.recyclerviewtemplate D/AndroidRuntime: Shutting down VM 
04-09 10:35:23.133 15155-15155/at.wurz.david.recyclerviewtemplate E/AndroidRuntime: FATAL EXCEPTION: main 
                        Process: at.wurz.david.recyclerviewtemplate, PID: 15155 
                        java.lang.RuntimeException: Unable to start activity ComponentInfo{at.wurz.david.recyclerviewtemplate/at.wurz.david.recyclerviewtemplate.RecyclerViewActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference 
                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2462) 
                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) 
                         at android.app.ActivityThread.access$800(ActivityThread.java:167) 
                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1419) 
                         at android.os.Handler.dispatchMessage(Handler.java:111) 
                         at android.os.Looper.loop(Looper.java:194) 
                         at android.app.ActivityThread.main(ActivityThread.java:5546) 
                         at java.lang.reflect.Method.invoke(Native Method) 
                         at java.lang.reflect.Method.invoke(Method.java:372) 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964) 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759) 
                        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference 
                         at at.wurz.david.recyclerviewtemplate.RecyclerViewActivity.onCreate(RecyclerViewActivity.java:76) 
                         at android.app.Activity.performCreate(Activity.java:5977) 
                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111) 
                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2415) 
                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)  
                         at android.app.ActivityThread.access$800(ActivityThread.java:167)  
                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1419)  
                         at android.os.Handler.dispatchMessage(Handler.java:111)  
                         at android.os.Looper.loop(Looper.java:194)  
                         at android.app.ActivityThread.main(ActivityThread.java:5546)  
                         at java.lang.reflect.Method.invoke(Native Method)  
                         at java.lang.reflect.Method.invoke(Method.java:372)  
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)  
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)  

我搜索了錯誤,但所有主題都至少1歲,並說谷歌已經修復了這個錯誤。

+0

哪裏錯誤堆棧跟蹤? –

+0

已編輯!如果你可以閱讀它的東西......:D – SomeRandomNameCuzWhyNot

+0

你已經實例化RecyclerView.LayoutManager rvLayoutManager; – Jois

回答

0

乾淨而建,它應該修復錯誤,並嘗試添加支持庫再次