2016-12-25 53 views
1

我想在我的android應用程序中使用CardView,我正在按照第二個教程,並仍然有相同的方法相同的錯誤。你能指出我做錯了什麼嗎?NPE在RecyclerView.setLayoutManager

這裏的錯誤代碼:

java.lang.RuntimeException: Unable to start activity ComponentInfo{co.scuer.licznik/co.scuer.licznik.EventsActivity}: 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: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.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference 
    at co.scuer.licznik.EventsFragment.onViewCreated(EventsFragment.java:46) 
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:988) 
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148) 
    at android.app.BackStackRecord.run(BackStackRecord.java:793) 
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535) 
    at android.app.FragmentController.execPendingActions(FragmentController.java:325) 
    at android.app.Activity.performStart(Activity.java:6252) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 
    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)  

下面是從EventsFragment所提到的方法:

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    EventObject[] objects = (EventObject[]) getArguments().getSerializable(OBJECTS_KEY); 

    eventsRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2)); 
    eventsRecyclerView.setAdapter(new EventsAdapter(objects)); 

} 

EventsAdapter.java:

package co.scuer.licznik; 

import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 


public class EventsAdapter extends RecyclerView.Adapter<EventsAdapter.EventsViewHolder> { 


    private final EventObject[] events; 

    public EventsAdapter(EventObject[] events) { 
     this.events = events; 
    } 

    @Override 
    public EventsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 


     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_event, parent, false); 
     return new EventsViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(EventsViewHolder holder, int position) { 

    } 

    @Override 
    public int getItemCount() { 
     return events.length; 
    } 

    class EventsViewHolder extends RecyclerView.ViewHolder { 

     public EventsViewHolder(View itemView) { 
      super(itemView); 
     } 
    } 
} 

而且item_event.xml:我好像

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <ImageView 
      android:id="@+id/itemImageView" 
      android:background="@android:color/black" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <TextView 
      android:id="@+id/itemTextView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 


    </LinearLayout> 

</android.support.v7.widget.CardView> 

無法得到解決這個問題。告訴我是否需要發佈更多代碼。

編輯:

我重寫onCreateView:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_events, container, false); 
    ButterKnife.bind(this, view); 
    return view; 
} 
+1

你需要做同樣的'查看視圖= LayoutInflater.from(parent.getContext())......'你片段和初始化裏面''recyclerview' onViewCreated' –

+0

你重寫onCreateView? – Rahul

回答

4

首先初始化的片段視圖。然後初始化您的UI組件。

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
super.onViewCreated(view, savedInstanceState); 

// Init your RecyclerView here 
    RecyclerView eventsRecyclerView = (RecyclerView)view.findViewById(R.id.your_viewid); 

    EventObject[] objects = (EventObject[]) getArguments().getSerializable(OBJECTS_KEY); 
    eventsRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2)); 
    eventsRecyclerView.setAdapter(new EventsAdapter(objects)); 
} 
+0

它幫助,謝謝 – krzk