public class JsonViewpager {
private VolleySingleton mVolleySingleton;
private ViewpagerAdapter mViewpagerAdapter;
private RequestQueue mRequestQueue;
private String mUrl;
private ArrayList<QuestionData> mQuestionDataArrayList = new ArrayList<QuestionData>();
public JsonViewpager(ViewpagerAdapter viewpagerAdapter,String url){
mViewpagerAdapter=viewpagerAdapter;
mUrl=url;
}
public void JsonRequestMethod() {
mVolleySingleton = VolleySingleton.getInstance();
mRequestQueue = mVolleySingleton.getRequestQueue();
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, mUrl, (String) null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Parse parseJSONResponse=new Parse();
mQuestionDataArrayList = parseJSONResponse(response);
mViewpagerAdapter.setBloglist(mQuestionDataArrayList);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
mRequestQueue.add(request);
}
}
我有另一个类,我在其中传递一个viewPagerAdapter,我在我的其他类的onCreateView中设置了viewPagerAdapter,由于某种原因,我在mViewpagerAdapter.setBloglist(mQuestionDataArrayList)时得到一个空指针异常;线。有谁知道造成这种情况的原因是什么?
家庭课程
public class Home extends Fragment {
private static final int REFRESH_DELAY=500;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
//URL reflects ordered list on server filter terms {"include": {"relation": "questions", "scope": {"order" : "points DESC"}}})
private static final String URL_HOME="hiddenUrl";
private String mParam1;
private String mParam2;
private ImageView mImageView;
private VolleySingleton mVolleySingleton;
private RequestQueue mRequestQueue;
private RecyclerView mRecyclerView;
private ArrayList<QuestionData> mListblogs = new ArrayList<>();
private ArrayList<QuestionData> mQuestionDataArrayList = new ArrayList<QuestionData>();
private PullToRefreshView mPullToRefreshView;
private ViewpagerAdapter mViewpagerAdapter;
public static Home newInstance(String param1, String param2) {
Home fragment = new Home();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public Home() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
//If you wish to handle you event source in the activity main thread, you’ll need to add the following instructions at the beginning of the onCreate() android callback:
System.out.println("userId" + Login.getUserId());
System.out.println("username" + Login.getUserName());
JsonViewpager s=new JsonViewpager(mViewpagerAdapter,URL_HOME);
s.JsonRequestMethod();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.fragment_dashboard_recycler);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mViewpagerAdapter = new ViewpagerAdapter(getActivity());
mRecyclerView.setAdapter((mViewpagerAdapter));
mPullToRefreshView = (PullToRefreshView) view.findViewById(R.id.pull_to_refresh);
mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
@Override
public void onRefresh() {
mPullToRefreshView.postDelayed(new Runnable() {
@Override
public void run() {
mPullToRefreshView.setRefreshing(false);
JsonRequestMethod();
}
}, REFRESH_DELAY);
}
});
return view;
}
}
答案 0 :(得分:1)
您正在致电
JsonViewpager s=new JsonViewpager(mViewpagerAdapter,URL_HOME);
在onCreate
方法中,然后
mViewpagerAdapter = new ViewpagerAdapter(getActivity());
在onCreateView
方法中。
问题在于,在活动生命周期中,onCreateVeiw
将在onCreate
之后出现,这意味着您的JsonViewepager对象已创建,传递的值为mViewpagerAdapter
。