gpt4 book ai didi

java - Fragment中RecyclerView使用setAdapter时出错

转载 作者:行者123 更新时间:2023-12-01 11:08:32 25 4
gpt4 key购买 nike

我想在 Fragment 中使用 recyclerView 来解析 JSON,一切工作正常,但是当我尝试设置 recyclerView 适配器时,它会给出错误

这是MainActivity的代码

 protected void onPostExecute(Integer result) {
// Download complete. Let us update UI
// homeFragment.mProgressBar.setVisibility(View.GONE);

if (result == 1) {
new Thread(new Runnable() {
@Override
public void run() {
homeFragment.mAdapter = new MembersAdapter(homeFragment.membersList);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
homeFragment.mRecyclerView.setAdapter(homeFragment.mAdapter);
}
});
}
}).start();

这是 fragment HomeFragment

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

View home_view = inflater.inflate(R.layout.list_view, container, false);

mRecyclerView = (RecyclerView) home_view.findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());

mProgressBar = (ProgressBar) home_view.findViewById(R.id.progress_bar);
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.GONE);


MainActivity activity = (MainActivity) getActivity();

activity.Senturl = "http://javatechig.com/?json=get_recent_posts&count=45";

activity.download.execute(activity.Senturl);

return home_view;

这是适配器和 View 支架

public class MembersAdapter extends RecyclerView.Adapter<MembersAdapter.MemberViewHolder>
{
private List<Members> membersList;
private Context mContext;
public MembersAdapter( List <Members> membersList)
{
this.membersList=membersList;
//this.mContext=context;
}
@Override
public MemberViewHolder onCreateViewHolder (ViewGroup parent,int viewType)
{
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, null);
MemberViewHolder memberHolder =new MemberViewHolder(view);
return memberHolder;
}
@Override
public void onBindViewHolder (MemberViewHolder holder,int position)
{
Members member = membersList.get(position);

Picasso.with(mContext).load(member.getThumbnail()).error(R.drawable.games).placeholder(R.drawable.about).into(holder.imageView);
holder.textView.setText(Html.fromHtml(member.getTitle()));
//holder.tvShort_Description.setText(member.getShort_description());
//holder.tvDate.setText(member.getDate());

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

public class MemberViewHolder extends RecyclerView.ViewHolder
{
protected TextView textView;//,tvShort_Description,tvDate;
public ImageView imageView;

public MemberViewHolder(View view)
{
super (view);
this.imageView=(ImageView)view.findViewById(R.id.thumbnail);
this.textView=(TextView)view.findViewById(R.id.title);
}

}

}

最后这是错误开始的 logcat

09-18 10:27:26.769    2161-2161/com.dushyant30suthar.gitsevent W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa62f2288)
09-18 10:27:26.769 2161-2161/com.dushyant30suthar.gitsevent E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException
at com.dushyant30suthar.gitsevent.MainActivity$MainActivityData$1$1.run(MainActivity.java:201) at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

直接在postExceute方法中设置Adapter即可

protected void onPostExecute(Integer result) {

if (result == 1) {

homeFragment.mAdapter = new MembersAdapter(homeFragment.membersList);

homeFragment.mRecyclerView.setAdapter(homeFragment.mAdapter);

}
}).start();

关于java - Fragment中RecyclerView使用setAdapter时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32650009/

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