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

时间:2015-07-06 10:23:49

标签: java android android-adapter

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

logcat的:

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);
}}

1 个答案:

答案 0 :(得分:0)

您的错误是因为您在空字符串上调用Html.fromHtml。 在调用listItems.getPostId()之前,您应该测试Html.fromHtml的值。