我有一个ViewPager在视图分页器中有3个列表,每个列表都有一个简单的SwipeRefreshLayout它运行良好但是当RefreshLayout重新填充并按下后退按钮(onPause我认为?)程序将崩溃,错误是你不能设置null对象我知道问题我不知道如何解决它 请帮助这是我的代码
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.activity_fragment1,container,false);
mListView = (ListView) view.findViewById(R.id.list_news);
refresh = (SwipeRefreshLayout) view.findViewById(R.id.refresh);
refresh.setColorSchemeColors(
R.color.material_green_200,
R.color.material_green_400,
R.color.material_green_600,
R.color.material_green_800
);
refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
String uri = "http://192.168.1.101/mySite/Flowers/flowers.json";
JsonArrayRequest request = new JsonArrayRequest(uri,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JsonParser parser = new JsonParser();
ArrayList items = parser.parseJson(response);
FlowerAdapter adapter = new FlowerAdapter(getContext(),
R.layout.activity_last_news_fragment,items);
mListView.setAdapter(adapter);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "error", Toast.LENGTH_SHORT).show();
}
});
RequestQueue quew = Volley.newRequestQueue(getActivity().getApplicationContext());
quew.add(request);
refresh.setRefreshing(false);
}
});
String uri = "http://192.168.1.101/mySite/Flowers/flowers.json";
JsonArrayRequest request = new JsonArrayRequest(uri,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JsonParser parser = new JsonParser();
ArrayList items = parser.parseJson(response);
FlowerAdapter adapter = new FlowerAdapter(getContext(),
R.layout.activity_last_news_fragment,items);
mListView.setAdapter(adapter);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "error", Toast.LENGTH_SHORT).show();
}
});
RequestQueue quew = Volley.newRequestQueue(getActivity().getApplicationContext());
quew.add(request);
return view;
}
@Override
public void onPause() {
if (refresh.isRefreshing()){
refresh.setEnabled(false);
}else {
super.onPause();
}
}
如何覆盖onpause方法,以便在第一次停止刷新然后暂停时停止?
这是竞争错误日志
12-07 17:11:41.942 13992-13992/app.mma.introsliderproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: app.mma.introsliderproject, PID: 13992
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.support.v4.app.FragmentActivity.getApplicationContext()' on a null object reference
at app.mma.PokerInfo.twitch.TwitchFragment$2.onResponse(TwitchFragment.java:73)
at app.mma.PokerInfo.twitch.TwitchFragment$2.onResponse(TwitchFragment.java:67)
at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7409)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:1)
这导致Volley.newRequestQueue(getActivity().getApplicationContext());
内的问题onRefresh()
。您需要检查您的活动是否处于运行状态,即上下文是否为空。
@Override
public void onRefresh () {
if (YourFragment.this.isVisible()) {
String uri = "http://192.168.1.101/mySite/Flowers/flowers.json";
JsonArrayRequest request = new JsonArrayRequest(uri,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JsonParser parser = new JsonParser();
ArrayList items = parser.parseJson(response);
FlowerAdapter adapter = new FlowerAdapter(getContext(),
R.layout.activity_last_news_fragment,items);
mListView.setAdapter(adapter);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "error", Toast.LENGTH_SHORT).show();
}
});
RequestQueue quew = Volley.newRequestQueue(getActivity().getApplicationContext());
quew.add(request);
refresh.setRefreshing(false);
}
}
将所有内容放在if(){}
内以避免崩溃