1

我是Android的初學者。我試圖在片段中使用Recycler View作爲導航抽屜的一部分。以下是代碼片段:沒有適配器連接;跳過佈局錯誤

RecyclerFragment.java

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.recyclerfragment,container,false); 


    navTitles = getResources().getStringArray(R.array.navDrawerItems); 
    navIcons = getResources().obtainTypedArray(R.array.navDrawerIcons); 
    recyclerViewAdapter = new Adapter(navTitles,navIcons,this.activity); 
    recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView); 
    recyclerView.setAdapter(recyclerViewAdapter); 
    recyclerView.setLayoutManager(new LinearLayoutManager(this.activity)); 
    return v; 
} 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
    this.activity=(MainActivity)context; 
} 
@Override 
    public void onAttach(Activity activity){ 
    super.onAttach(activity); 
    this.activity=activity; 


} 

和適配器代碼:

Adapter(String[] titles , TypedArray icons , Context context){ 

    this.titles = titles; 
    this.icons = icons; 
    this.context = context; 
    //inflator=LayoutInflater.from(context); 
} 

public class ViewHolder extends RecyclerView.ViewHolder { 

    TextView navTitle; 
    ImageView navIcon; 
    Context context; 

    public ViewHolder(View drawerItem , int itemType , Context context){ 

     super(drawerItem); 
     this.context = context; 
     // drawerItem.setOnClickListener(this); 
     if(itemType==1){ 
      navTitle = (TextView) itemView.findViewById(R.id.tv_NavTitle); 
      navIcon = (ImageView) itemView.findViewById(R.id.iv_NavIcon); 
     } 
    } 
} 

@Override 
public Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

    LayoutInflater layoutInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    if(viewType==1){ 
     View itemLayout = layoutInflater.inflate(R.layout.drawer_item_layout,null); 
     return new ViewHolder(itemLayout,viewType,context); 
    } 
    else if (viewType==0) { 
     View itemHeader = layoutInflater.inflate(R.layout.header_layout,null); 
     return new ViewHolder(itemHeader,viewType,context); 
    } 
    return null; 
} 
@Override 
public void onBindViewHolder(Adapter.ViewHolder holder, int position) { 

    if(position!=0){ 
     holder.navTitle.setText(titles[position - 1]); 
     holder.navIcon.setImageResource(icons.getResourceId(position-1,-1)); 
    } 
} 
@Override 
public int getItemCount() { 
    return titles.length+1; 
} 
@Override 
public int getItemViewType(int position) { 
    if(position==0)return 0; 
    else return 1; 
} 

但是當我運行該應用程序我得到的日誌貓:

02-27 20:38:41.848 25007-25007/com.example.hp.recyclernavigation E/RecyclerView: No adapter attached; skipping layout 
02-27 20:38:47.693 25007-25007/com.example.hp.recyclernavigation E/RecyclerView: No adapter attached; skipping layout 
02-27 20:39:13.730 25007-25007/com.example.hp.recyclernavigation E/RecyclerView: No adapter attached; skipping layout 
02-27 20:39:13.816 25007-25007/com.example.hp.recyclernavigation E/RecyclerView: No adapter attached; skipping layout 
02-27 20:39:14.271 25007-25007/com.example.hp.recyclernavigation E/AndroidRuntime: FATAL EXCEPTION: main 
                       Process: com.example.hp.recyclernavigation, PID: 25007 
                       java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference 
                        at android.support.v7.widget.RecyclerView.computeVerticalScrollRange(RecyclerView.j ava:1581) 
                        at android.view.View.onDrawScrollBars(View.java:12837) 
                        at android.view.View.draw(View.java:15138) 
                        at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:3171) 
                        at android.view.View 

有人可以幫助我解決這個問題。

編輯:

這裏是activity_main.xml中:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:local="http://schemas.android.com/apk/res-auto" 
android:id="@+id/drawerMainActivity" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

    <LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <include android:id="@+id/toolBar" 
     layout="@layout/app_bar"/> 
    </LinearLayout> 
    <FrameLayout 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:id="@+id/containerView"> 


    </FrameLayout> 
    <fragment 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:name="com.example.hp.recyclernavigation.RecyclerFragment" 
    android:id="@+id/fragment" 
    > 

    <android.support.v7.widget.RecyclerView 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:id="@+id/recyclerView" 
     android:scrollbars="vertical" 
     android:background="#FFFFFF" 
     android:layout_gravity="left" 
     /> 
</fragment> 
</android.support.v4.widget.DrawerLayout> 
+0

你已經把相同的代碼兩次 –

+0

@ helldawg13我已經在這個問題 – ghostrider

回答

2

修改這樣的代碼:

recyclerViewAdapter = new Adapter(navTitles,navIcons,getActivity().getApplicationContext()); 
recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView); 
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext())); 
recyclerView.setAdapter(recyclerViewAdapter); 

首先,你必須設置佈局管理器,然後將適配器! !

+0

做了改變,但我仍然得到同樣的錯誤變化......這是工作正常時,recyclerview獨自一人,但我加了它在片段裏面,事情開始搞砸了! – ghostrider

+0

請參閱我的編輯....您的上下文必須錯誤....嘗試getActivity()而不是this.activity –

+0

做了同樣的事情,但仍然存在錯誤...是否與RecyclerView $ LayoutManager.canScrollVertically ()? – ghostrider

相關問題