我是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>
你已經把相同的代碼兩次 –
@ helldawg13我已經在這個問題 – ghostrider