在操作期间AsyncTask我在AsyncTask完成之前去新片之前我去了一个新片段,我看到与Context相关的错误。
例如: 我在包含列表的片段上。运行我的AsyncTask并立即转到另一个片段。我看到一个错误:
12-24 03:03:50.678 1516-1516/com.managment.pavel.managmentgradle E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.view.LayoutInflater.from(LayoutInflater.java:171)
at com.managment.pavel.managmentgradle.fragments.adapters.GuestsListAdapter.<init>(GuestsListAdapter.java:40)
at com.managment.pavel.managmentgradle.fragments.GuestsList$1.onPostExecute(GuestsList.java:141)
at com.managment.pavel.managmentgradle.fragments.GuestsList$1.onPostExecute(GuestsList.java:91)
at android.os.AsyncTask.finish(AsyncTask.java:417)
at android.os.AsyncTask.access$300(AsyncTask.java:127)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
错误就行:
inflater = LayoutInflater.from(ctx);
完整代码适配器:
public class GuestsListAdapter extends BaseAdapter implements View.OnClickListener {
private LayoutInflater inflater;
private ArrayList<GuestBean> guests;
private Context ctx;
private DisplayImageOptions options;
private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
public GuestsListAdapter(Context ctx, ArrayList<GuestBean> guests) {
this.guests = guests;
this.ctx = ctx;
inflater = LayoutInflater.from(ctx);
options = ILOptions.getOption();
}
@Override
public int getCount() {
return guests.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
if (convertView == null) {
view = inflater.inflate(R.layout.itemguest, parent, false);
holder = new ViewHolder();
holder.number = (TextView) view.findViewById(R.id.roomNumber);
holder.name = (TextView) view.findViewById(R.id.guestName);
holder.image = (CircleImageView) view.findViewById(R.id.imageView);
holder.balance = (TextView) view.findViewById(R.id.guestBalance);
holder.company = (TextView) view.findViewById(R.id.guestСompany);
holder.night = (TextView) view.findViewById(R.id.guestNight);
holder.wifi = (ImageView) view.findViewById(R.id.wifiImg);
holder.inroom = (ImageView) view.findViewById(R.id.inroomImg);
holder.cleaning = (ImageView) view.findViewById(R.id.cleaningImg);
holder.tv = (ImageView) view.findViewById(R.id.tvImg);
holder.checkin = (TextView) view.findViewById(R.id.checkinText);
holder.tarif = (TextView) view.findViewById(R.id.tarifText);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.number.setText("Комната № " + guests.get(position).getRoom());
holder.name.setText("ФИО: " + guests.get(position).getName());
ImageLoader.getInstance().displayImage(guests.get(position).getImage(), holder.image, options, animateFirstListener);
holder.balance.setText("Баланс: " + guests.get(position).getBalance());
holder.tarif.setText("Тариф: " + guests.get(position).getTarif());
if (Float.parseFloat(guests.get(position).getBalance())>=0.0) {
holder.balance.setTextColor(ctx.getResources().getColor(R.color.balance_positive));
}else{
holder.balance.setTextColor(ctx.getResources().getColor(R.color.balance_negative));
}
if (!guests.get(position).getCompany().equals("")) {
holder.company.setText("Компания: " + guests.get(position).getCompany());
}else{
holder.company.setText("Компания: не указано");
}
if (!guests.get(position).getTotalNigth().equals("")) {
holder.night.setText("Ночей: " + guests.get(position).getTotalNigth());
}else{
holder.night.setText("Ночей: не указано");
}
if (guests.get(position).isWifi()) {
holder.wifi.setVisibility(View.VISIBLE);
} else {
holder.wifi.setVisibility(View.INVISIBLE);
}
if (guests.get(position).isInroom()) {
holder.inroom.setImageResource(R.drawable.human_on);
} else {
holder.inroom.setImageResource(R.drawable.human_off);
}
if (guests.get(position).isCleaning()) {
holder.cleaning.setVisibility(View.VISIBLE);
} else {
holder.cleaning.setVisibility(View.INVISIBLE);
}
if (guests.get(position).isTv()) {
holder.tv.setVisibility(View.VISIBLE);
} else {
holder.tv.setVisibility(View.INVISIBLE);
}
if (guests.get(position).getCheckins().equals("0")) {
holder.checkin.setVisibility(View.INVISIBLE);
}
if (guests.get(position).getCheckins().equals("")) {
holder.checkin.setText("R?");
}
if (!guests.get(position).getCheckins().equals("") && !guests.get(position).getCheckins().equals("")) {
holder.checkin.setText("R" + guests.get(position).getCheckins());
}
holder.cleaning.setOnClickListener(this);
return view;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.cleaningImg:
Toast.makeText(ctx, "уборка", Toast.LENGTH_LONG).show();
break;
}
}
private static class ViewHolder {
TextView number;
TextView name;
CircleImageView image;
TextView balance;
TextView company;
TextView night;
ImageView wifi;
ImageView inroom;
ImageView cleaning;
ImageView tv;
TextView checkin;
TextView tarif;
}
}
调用适配器的地方:
private ArrayList<GuestBean> guests;
..............
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");
Elements elementsNames = document.select("name");
Elements elementsImageLinks = document.select("photo");
Elements elementsBalance = document.select("balance");
Elements elementsCompany = document.select("company");
Elements elementsCountry = document.select("country");
Elements elementsWiFi = document.select("wifi");
Elements elementsInroom = document.select("inroom");
Elements elementsCleaning = document.select("cleaning");
Elements elementsTv = document.select("tv");
Elements elementsTotalNigth = document.select("total_nights");
Elements elementsCheckins = document.select("checkins");
Elements elementsCheckin = document.select("checkin");
Elements elementsCheckout = document.select("checkout");
Elements elementsTarif = document.select("tariff");
Elements elementsNigth = document.select("nights");
for (int i = 0; i < elementsNumbers.size(); i++) {
GuestBean guest = new GuestBean();
guest.setUserId(elementsUserId.get(i).ownText());
guest.setRoom(elementsNumbers.get(i).ownText());
guest.setName(elementsNames.get(i).ownText());
guest.setImage(elementsImageLinks.get(i).ownText());
guest.setBalance(elementsBalance.get(i).ownText());
guest.setCompany(elementsCompany.get(i).ownText());
guest.setCountry(elementsCountry.get(i).ownText());
guest.setWifi(Boolean.parseBoolean(elementsWiFi.get(i).ownText()));
guest.setInroom(Boolean.parseBoolean(elementsInroom.get(i).ownText()));
guest.setCleaning(Boolean.parseBoolean(elementsCleaning.get(i).ownText()));
guest.setTv(Boolean.parseBoolean(elementsTv.get(i).ownText()));
guest.setTotalNigth(elementsTotalNigth.get(i).ownText());
guest.setCheckins(elementsCheckins.get(i).ownText());
guest.setCheckin(elementsCheckin.get(i).ownText());
guest.setCheckout(elementsCheckout.get(i).ownText());
guest.setTarif(elementsTarif.get(i).ownText());
guest.setNigth(elementsNigth.get(i).ownText());
guests.add(guest);
count++;
}
GuestsListAdapter adapter = new GuestsListAdapter(getActivity(), guests);
list.setAdapter(adapter);
progressDialog.cancel();
swipeLayout.setRefreshing(false);
header = "Гости " + hotel + "" + "(" + count + ")";
getActivity().setTitle(header);
}
}.execute(link);
}