我想在Fragment中使用recyclelerView解析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();
这里是片段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;
这是适配器和查看持有人
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)
答案 0 :(得分:1)
只需在直接postExceute方法中设置适配器
protected void onPostExecute(Integer result) {
if (result == 1) {
homeFragment.mAdapter = new MembersAdapter(homeFragment.membersList);
homeFragment.mRecyclerView.setAdapter(homeFragment.mAdapter);
}
}).start();
答案 1 :(得分:0)
在调用setAdapter
之前检查homeFragment.mAdapter
是否为空