gpt4 book ai didi

java - 尝试在空对象引用上调用虚拟方法 'int java.lang.String.length()'

转载 作者:太空狗 更新时间:2023-10-29 14:56:09 25 4
gpt4 key购买 nike

<分区>

所以我有带有 Tabs 和 RecyclerView 的 Android 应用程序。当我运行我的应用程序时,它崩溃了。

日志:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at java.io.StringReader.<init>(StringReader.java:47)
at android.text.HtmlToSpannedConverter.convert(Html.java:442)
at android.text.Html.fromHtml(Html.java:136)
at android.text.Html.fromHtml(Html.java:99)
at com.example.app.MyRecyclerViewAdapter2.onBindViewHolder(MyRecyclerViewAdapter2.java:80)
at com.example.app.MyRecyclerViewAdapter2.onBindViewHolder(MyRecyclerViewAdapter2.java:17)
...

at com.example.app.MyRecyclerViewAdapter2.onBindViewHolder(MyRecyclerViewAdapter2.java:80)

指向这行代码

listRowViewHolder.postId.setText(Html.fromHtml(listItems.getPostId())); // This line of code leads to crashing of app

如果我删除此行,我的应用将正常运行。

MyRecyclerViewAdapter2.java:

    package com.example.app;


import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;

import java.util.List;

public class MyRecyclerViewAdapter2 extends RecyclerView.Adapter<ListRowViewHolder> {
private List<ListItems> listItemsList;
private Context mContext;
private ImageLoader mImageLoader;
private int focusedItem = 0;

public MyRecyclerViewAdapter2(Context context, List<ListItems> listItemsList) {
this.mContext = context;
this.listItemsList = listItemsList;
}

@Override
public ListRowViewHolder onCreateViewHolder(final ViewGroup viewGroup, int position) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_view_row2, null);

ListRowViewHolder holder = new ListRowViewHolder(v);

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView postUrl = (TextView) v.findViewById(R.id.postId);
String post_Id = postUrl.getText().toString();

Intent intent = new Intent(mContext, ViewPostActivity.class);
intent.putExtra("id", post_Id);
mContext.startActivity(intent);

}
});

return holder;
}

@Override
public void onBindViewHolder(ListRowViewHolder listRowViewHolder, int position) {
ListItems listItems = listItemsList.get(position);
listRowViewHolder.itemView.setSelected(focusedItem == position);

listRowViewHolder.getLayoutPosition();

mImageLoader = MySingleton.getInstance(mContext).getImageLoader();
listRowViewHolder.thumbnail.setImageUrl(listItems.getThumbnail(), mImageLoader);
listRowViewHolder.thumbnail.setDefaultImageResId(R.drawable.website_placeholder);

listRowViewHolder.title.setText(Html.fromHtml(listItems.getTitle()));
listRowViewHolder.date.setText(Html.fromHtml(listItems.getDate()));
listRowViewHolder.content.setText(Html.fromHtml(listItems.getContent()));
listRowViewHolder.postId.setText(Html.fromHtml(listItems.getPostId())); // This line of code leads to crashing of app
}

public void clearAdapter () {
listItemsList.clear();
notifyDataSetChanged();
}


@Override
public int getItemCount() {
return (null != listItemsList ? listItemsList.size() : 0);
}}

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