2015-02-23 165 views
0

我在我的應用程序中使用RecyclerView。我的應用程序崩潰,出現以下錯誤應用程序崩潰使用RecyclerView

顯示java.lang.NullPointerException:試圖調用虛擬方法無效android.support.v7.widget.RecyclerView $ LayoutManager.onMeasure(android.support.v7.widget.RecyclerView $回收站,android.support.v7.widget.RecyclerView $州,INT,INT)」的空對象引用

在計算器上,我發現,不添加布局管理器時,這個錯誤通常是搜索。但我添加了佈局管理器。請幫我看看是什麼導致了這次事故。

編輯 - 代碼使用Recyclerview

public class FeedFragment extends Fragment implements HJAppConstants { 

    private RecyclerView mRecyclerView; 

    private FeedRecyclerAdapter mAdapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.hj_feed_layout, null); 
     mRecyclerView = (RecyclerView) view 
       .findViewById(R.id.feeds_recycler_view); 
     return view; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     LinearLayoutManager layoutManager = new LinearLayoutManager(
       getActivity()); 
     layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
     mRecyclerView.setLayoutManager(layoutManager); 
     mAdapter = new FeedRecyclerAdapter(); 
     mRecyclerView.setAdapter(mAdapter); 
     addData(); 

    } 

    /** 
    * Adds static data 
    */ 
    private void addData() { 
     ArrayList<HJFeedItemObject> itemObjs = new ArrayList<HJFeedItemObject>(); 
     itemObjs.add(new HJFeedItemObject(HJ_FEED_TYPE_PHOTO, 
       "potala_palace.jpg", "This is an article about Potala Palace", 
       "Thu 7, 07:45pm", new HJUserObject("Umesh", ""))); 
     itemObjs.add(new HJFeedItemObject(HJ_FEED_TYPE_PRODUCT, "pokhara.jpg", 
       "A beautiful place to visit - Pokhara ", "Thu 7, 07:45pm", 
       new HJUserObject("Umesh", ""))); 
     itemObjs.add(new HJFeedItemObject(HJ_FEED_TYPE_ARTICLE, "patan.jpg", 
       "A Tourist attraction - Patan", "Thu 7, 07:45pm", 
       new HJUserObject("Umesh", ""))); 
     itemObjs.add(new HJFeedItemObject(HJ_FEED_TYPE_PHOTO, 
       "potala_palace.jpg", "This is an article about Potala Palace", 
       "Thu 7, 07:45pm", new HJUserObject("Umesh", ""))); 
     itemObjs.add(new HJFeedItemObject(HJ_FEED_TYPE_PRODUCT, "pokhara.jpg", 
       "A beautiful place to visit - Pokhara ", "Thu 7, 07:45pm", 
       new HJUserObject("Umesh", ""))); 
     itemObjs.add(new HJFeedItemObject(HJ_FEED_TYPE_ARTICLE, "patan.jpg", 
       "A Tourist attraction - Patan", "Thu 7, 07:45pm", 
       new HJUserObject("Umesh", ""))); 
     mAdapter.addData(itemObjs); 
    } 
+0

發佈您的代碼。 – 2015-02-23 12:19:03

+0

顯示代碼沒有辦法幫助沒有它 – 2015-02-23 12:19:13

+0

我已經發布我的代碼 – 2015-02-23 12:29:10

回答

0

時爲RecyclerView未提供佈局管理出現此問題的片段。

更改您的代碼,並將layoutManager設置移至onCreateView。

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState){ 

    View view = inflater.inflate(R.layout.hj_feed_layout, null); 
    mRecyclerView = (RecyclerView) view 
       .findViewById(R.id.feeds_recycler_view); 

    LinearLayoutManager layoutManager = new LinearLayoutManager(
       getActivity()); 
     layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
     mRecyclerView.setLayoutManager(layoutManager); 

    return view; 
}