当我在网络wifi工作时,程序表现完美。但如果我在移动互联网网络中,我们经常会收到错误。它出现在不同时间的不同片段中,但总是在同一个地方。
这是我的AsyncTask:
public class MyAsincTask extends AsyncTask<String, Void, Document> {
private Document document;
private final ProgressDialog progressDialog;
public MyAsincTask(Context context) {
progressDialog = MyProgress.getProgressDialog(context);
}
@Override
protected void onPreExecute() {
progressDialog.show();
super.onPreExecute();
}
@Override
protected Document doInBackground(String... params) {
try {
document = Jsoup.connect(params[0]).get();
} catch (IOException e) {
e.printStackTrace();
}
return document;
}
@Override
protected void onPostExecute(Document document) {
progressDialog.cancel();
super.onPostExecute(document);
}
}
所以我称之为:
public void setData(){
new MyAsincTask(getActivity()){
@Override
protected void onPreExecute() {
count=0;
}
@Override
protected void onPostExecute(Document document) {
super.onPostExecute(document);
guests = new ArrayList<>();
Elements elementsUserId = document.select("guest_id");
Elements elementsNumbers = document.select("room");
for (int i = 0; i < elementsNumbers.size(); i++) {
GuestBean guest = new GuestBean();
guest.setUserId(elementsUserId.get(i).ownText());
guest.setRoom(elementsNumbers.get(i).ownText());
guests.add(guest);
count++;
}
if (getActivity()!=null) {
GuestsListAdapter adapter = new GuestsListAdapter(guests);
list.setAdapter(adapter);
progressDialog.cancel();
swipeLayout.setRefreshing(false);
header = "Гости " + hotel + "" + "(" + count + ")";
getActivity().setTitle(header);
}
}
}.execute(link);
}
当我打开碎片时有时会出现此错误。它发生在不同的片段,但始终在相同的区域代码中:
01-06 19:55:49.765 11237-11237/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.managment.pavel.managmentgradle.fragments.GuestsList$1.onPostExecute(GuestsList.java:100)
at com.managment.pavel.managmentgradle.fragments.GuestsList$1.onPostExecute(GuestsList.java:91)
at android.os.AsyncTask.finish(AsyncTask.java:602)
at android.os.AsyncTask.access$600(AsyncTask.java:156)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4464)
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:822)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:589)
at dalvik.system.NativeStart.main(Native Method)
第91行:
new MyAsincTask(getActivity()){
第100行
Elements elementsUserId = document.select("guest_id");