gpt4 book ai didi

android - 从 IntentService 添加数据时,recyclerview 崩溃

转载 作者:行者123 更新时间:2023-11-30 01:38:47 25 4
gpt4 key购买 nike

当我尝试从 intentservice 添加新数据到 recyclerview 时,当从 intentservice 的 addMsg 函数调用以下行时,我收到以下错误。我该如何解决它?

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



}
});
}

最佳答案

恕我直言,您遇到了上下文问题。

 new SimpleStringRecyclerViewAdapter(getBaseContext()).notifyDataSetChanged();

Adapter 使用 context 获取 LayoutInflater 和 inflate Views,所以你需要传递 Activity context。

因此在 Activity onCreate 中创建适配器,将 Activity 作为上下文传递,并在接收消息时将新数据传递给旧适配器(不要创建新适配器)

关于android - 从 IntentService 添加数据时,recyclerview 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34785877/

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