java - 片段中 RecyclerView 中的 textView 不显示来自 Java 或 XML 文件的任何文本
问题描述
编辑了我的问题标题我正在尝试将 JSON 数据传递到内置在片段中的 RecyclerView 中,实际上我在装饰 RecyclerView 的 list_items.xml 之前就这样做了。
现在我正在尝试在此 Recycler View 中显示一些普通文本,并且 AS 总是在 MyViewHolder 行给我一个 NullPointerException ...在我评论该行之后,应用程序运行但 RecyclerView 甚至不显示任何文本RecyclerView 的 list_items.xml 中的文本
这是我到目前为止的结果,recyclerView 仅显示 ImageView。并且 textView 不起作用。
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {
private Context context; private List<ForumsListItem> forumsListItems;
ForumsFragment fragment=new ForumsFragment();
//Define the constructor for both contenxt and listItem
public MyRecyclerAdapter(Context context, List<ForumsListItem> items) {
this.context = context;
forumsListItems =items;
}
@NonNull
@Override
public MyRecyclerAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext())
.inflate(R.layout.forums_listitems,parent,false);
return new MyViewHolder(v);
}
/*Bind everything from te MyViewHolder*/
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
//Define listItem position
ForumsListItem listItem=forumsListItems.get(position);
//Calling the textView inside bindingViewHolder
//holder.catListName.setText(listItem.getCatlistName());
holder.numOfDailyNewPosts.setText(listItem.getNumOftodayPosts());
holder.descriptTv.setText(listItem.getDescription());
}
@Override
public int getItemCount() {
return forumsListItems.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView nameTv,descriptTv,numOfDailyNewPosts,catListName;
private ImageView imgIcons;
public MyViewHolder(View itemView) {
super(itemView);
//things to display in form of List
descriptTv=(TextView)itemView.findViewById(R.id.item_detail);
catListName=(TextView)itemView.findViewById(R.id.catListTxt);
numOfDailyNewPosts=(TextView)itemView.findViewById(R.id.numOfDailyNewPosts);
//can add Picaso loding images here
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v){
int pos=getAdapterPosition();
//get the current position
ForumsListItem items= forumsListItems.get(pos);
Toast.makeText(context,items.getForumsTitles(),Toast.LENGTH_LONG).show();
//On click functionality
}
}
}
我的 list_items.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/cardview_compat_inset_shadow"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginStart="5dp"
android:padding="16dp" >
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_light_yellow">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/forumsTitleImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:src="@android:drawable/checkbox_on_background"/>
<TextView
android:id="@+id/numOfDailyNewPosts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fdlksfjsdlkjf"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@id/forumsTitleImg"
android:layout_margin="10dp" />
</android.support.constraint.ConstraintLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/item_detail"
android:layout_margin="10dp"
app:layout_constraintTop_toBottomOf="@+id/frame"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
06-03 23:07:30.752 2964-2964/? 我/艺术:后期启用 -Xcheck:jni 06-03 23:07:30.930 2964-2964/com.yamibo.bbs.splashscreen W/艺术:执行失败(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art - -runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.yamibo.bbs.splashscreen- 2/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.yamibo.bbs.splashscreen-2@split_lib_dependencies_apk.apk@classes.dex) 因为非0退出状态06- 03 23:07:31.416 2964-2964/com.yamibo.bbs.splashscreen W/艺术:闪屏 I/时间线:时间线:Activity_idle id:android.os.BinderProxy@1c32706 时间:11829580 06-03 23:07:36.591 2964-2964/com.yamibo.bbs.splashscreen I/时间线:时间线:Activity_launch_request id:com。 yamibo.bbs.splashscreen 时间:11833307 06-03 23:07:37.435 2964-2964/com.yamibo.bbs.splashscreen D/TEST2: 10 06-03 23:07:37.460 2964-3123/com.yamibo.bbs。 splashscreen W/System:ClassLoader 引用未知路径:/system/framework/tcmclient.jar 06-03 23:07:37.692 2964-2964/com.yamibo.bbs.splashscreen E/RecyclerView:未连接适配器;跳过布局 06-03 23:07:37.773 2964-2964/com.yamibo.bbs.splashscreen I/Choreographer:跳过 40 帧!应用程序可能在其主线程上做了太多工作。06-03 23:07:37.980 2964-2964/com.yamibo.bbs.splashscreen W/art:在 Android 4.1 之前,方法 int android.support.v7。widget.DropDownListView.lookForSelectablePosition(int, boolean) 会错误地覆盖 android.widget.ListView 06-03 23:07:38.084 2964-2964/com.yamibo.bbs.splashscreen E/RecyclerView 中的包私有方法:没有适配器随附的; 跳过布局 06-03 23:07:38.112 2964-2964/com.yamibo.bbs.splashscreen I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@b5d7e1c time:11834828 06-03 23:07:38.672 2964- 3123/com.yamibo.bbs.splashscreen W/System.err:org.json.JSONException:org.json.JSONArray.get(JSONArray.java:293) 处 org.json.JSONArray.get(JSONArray.java:293) 处的索引 2 超出范围 [0..2)。 json.JSONArray.getJSONObject(JSONArray.java:521) 在 com.yamibo.bbs.splashscreen.ForumsFragment$ForumsJSONParser.doInBackground(ForumsFragment.java:150) 在 com.yamibo.bbs.splashscreen.ForumsFragment$ForumsJSONParser。
解决方案
@基拉:
请用以下代码替换您的 list_items.xml 文件并再次检查:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/forumsTitleImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:src="@android:drawable/checkbox_on_background" />
<TextView
android:id="@+id/numOfDailyNewPosts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="fdlksfjsdlkjf" />
</LinearLayout>
</FrameLayout>
<TextView
android:id="@+id/item_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
</LinearLayout>
</android.support.v7.widget.CardView></RelativeLayout>
这是带有静态数据的 Java 文件:
public class SampleActivity extends Activity{
RecyclerView sampleRecycle;
MyRecyclerAdapter myRecyclerAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_activity);
sampleRecycle = (RecyclerView) findViewById(R.id.sampleRecycle);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
sampleRecycle.setLayoutManager(mLayoutManager);
myRecyclerAdapter = new MyRecyclerAdapter(this);
sampleRecycle.setAdapter(myRecyclerAdapter);
}
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {
private Context context;
// private List<ForumsListItem> forumsListItems;
// ForumsFragment fragment = new ForumsFragment();
//Define the constructor for both contenxt and listItem
public MyRecyclerAdapter(Context context) {
this.context = context;
// forumsListItems = items;
}
@NonNull
@Override
public MyRecyclerAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_items, parent, false);
return new MyViewHolder(v);
}
/*Bind everything from te MyViewHolder*/
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
//Define listItem position
// ForumsListItem listItem = forumsListItems.get(position);
//Calling the textView inside bindingViewHolder
//holder.catListName.setText(listItem.getCatlistName());
// holder.numOfDailyNewPosts.setText(listItem.getNumOftodayPosts());
// holder.descriptTv.setText(listItem.getDescription());
}
@Override
public int getItemCount() {
return 20;
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView nameTv, descriptTv, numOfDailyNewPosts, catListName;
private ImageView imgIcons;
public MyViewHolder(View itemView) {
super(itemView);
//things to display in form of List
descriptTv = (TextView) itemView.findViewById(R.id.item_detail);
// catListName = (TextView) itemView.findViewById(R.id.catListTxt);
numOfDailyNewPosts = (TextView) itemView.findViewById(R.id.numOfDailyNewPosts);
//can add Picaso loding images here
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int pos = getAdapterPosition();
//get the current position
// ForumsListItem items = forumsListItems.get(pos);
// Toast.makeText(context, items.getForumsTitles(), Toast.LENGTH_LONG).show();
//On click functionality
}
}
}
}
推荐阅读
- parameters - Anylogic 参数变化实验:我以哪种方式使用“表达式”列在其允许的状态下改变布尔参数?
- node.js - 管理拍卖组件中的剩余时间
- java - Selenium 出现问题,无头运行时出现意外行为
- node.js - MongoDB/Mongoose 如何管理一些数字字段的增量
- android - IAP 未准备好。检查 Google Play 服务是否可用
- mysql - 将 Google 表格列映射到 MySQL 数据类型
- swift - 如何在 Swift 中获得标准输入/输出/错误的“管道”?
- java - 需要帮助正确打印出值而不在 java 中重复它们
- python - PySpark - 条件语句
- javascript - 使用 fetch API 在 GET 方法上发送参数时出错