gpt4 book ai didi

android - 未连接适配器;跳过布局错误

转载 作者:行者123 更新时间:2023-11-29 20:05:58 25 4
gpt4 key购买 nike

我是 Android 初学者。我正在尝试在 fragment 中使用 Recycler View 作为 Navigation Drawer 的一部分。以下是 fragment 代码:

回收器 fragment .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;
}

但是当我运行应用程序时,我得到了 Log Cat:

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>

最佳答案

像这样修改你的代码:

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

首先你必须设置布局管理器,然后设置适配器!!!

关于android - 未连接适配器;跳过布局错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35671455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com