当我从intentservice从addMsg函数调用以下行时,当我尝试从intentservice向recyclerview添加新数据时,我收到以下错误。我可以解决它吗?
new SimpleStringRecyclerViewAdapter(getBaseContext()).notifyDataSetChanged();
错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
at com.example.chat$SimpleStringRecyclerViewAdapter.<init>(chat.java:471)
at com.example.chat$8.run(chat.java:639)
完整代码:
public class CheeseListFragment extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat);
setupRecyclerView(rv);
}
private void setupRecyclerView(RecyclerView recyclerView) {
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getActivity(),
getRandomSublist(Cheeses.sCheeseStrings, 30)));
}
private List<String> getRandomSublist(String[] array, int amount) {
ArrayList<String> list = new ArrayList<>(amount);
Random random = new Random();
while (list.size() < amount) {
list.add(array[random.nextInt(array.length)]);
}
return list;
}
public static class SimpleStringRecyclerViewAdapter
extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {
private final TypedValue mTypedValue = new TypedValue();
private int mBackground;
private List<String> mValues;
public static class ViewHolder extends RecyclerView.ViewHolder {
public String mBoundString;
public final View mView;
public final ImageView mImageView;
public final TextView mTextView;
public ViewHolder(View view) {
super(view);
mView = view;
mImageView = (ImageView) view.findViewById(R.id.avatar);
mTextView = (TextView) view.findViewById(android.R.id.text1);
}
@Override
public String toString() {
return super.toString() + " '" + mTextView.getText();
}
}
public String getValueAt(int position) {
return mValues.get(position);
}
public SimpleStringRecyclerViewAdapter(Context context, List<String> items) {
context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
mBackground = mTypedValue.resourceId;
mValues = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
view.setBackgroundResource(mBackground);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mBoundString = mValues.get(position);
holder.mTextView.setText(mValues.get(position));
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, CheeseDetailActivity.class);
intent.putExtra(CheeseDetailActivity.EXTRA_NAME, holder.mBoundString);
context.startActivity(intent);
}
});
Glide.with(holder.mImageView.getContext())
.load(Cheeses.getRandomCheeseDrawable())
.fitCenter()
.into(holder.mImageView);
}
@Override
public int getItemCount() {
return mValues.size();
}
}
public void addMsg(String msg_text){
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
mValues.add(chatmodel);
new SimpleStringRecyclerViewAdapter(getBaseContext()).notifyDataSetChanged();
recyclerView.setLayoutManager(new LinearLayoutManager(chat.getContext()));
recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getBaseContext()));
recyclerView.scrollToPosition(mValues.add.size() - 1);
}
});
}
答案 0 :(得分:1)
恕我直言,你有上下文的问题。
new SimpleStringRecyclerViewAdapter(getBaseContext()).notifyDataSetChanged();
适配器使用上下文来获取LayoutInflater并膨胀视图,因此您需要传递Activity上下文。
因此,在Activity onCreate中创建适配器,将Activity作为上下文传递,并在接收消息上将新数据传递给旧适配器(不要创建新适配器)