如何在AsyncTask或Fragment中处理Null?

时间:2015-01-06 15:43:33

标签: android android-fragments android-asynctask

在我的AsyncTask中,我得到一个对象Document,以便在Fragment中与他进一步合作。问题是有时我得到document =NULL。我可以捕获此异常,但不知道如何处理它。让我们说我得到了一个空虚。但接下来该怎么办?如果我得到数据,我就形成了我的片段。但如果我得到一个Null,我不知道如何行动

我的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]).timeout(0).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return document;
    }

    @Override
    protected void onPostExecute(Document document) {
        progressDialog.cancel();
        super.onPostExecute(document);
    }
}

如果成功收到数据,我会形成我的片段。但如果你来Null怎么办?:

public void setData() {
        String link = String.format("http://core.android?key=pe=xml", guestid, hotel);
       new MyAsincTask(getActivity()){
           @Override
           protected void onPreExecute() {
               super.onPreExecute();
           }

           @Override
           protected void onPostExecute(Document document) {
               super.onPostExecute(document);
               Elements elementsId = document.select("id");
               Elements elementsCheckin = document.select("checkin");
               Elements elementsCheckout = document.select("checkout");
               Elements elementsNights = document.select("nights");

               for (int i = 0; i < elementsId.size(); i++) {
                   HistoryBean historyBean = new HistoryBean();
                   historyBean.setId(elementsId.get(i).ownText());
                   historyBean.setCheckin(elementsCheckin.get(i).ownText());
                   historyBean.setCheckout(elementsCheckout.get(i).ownText());
                   historyBean.setNigth(elementsNights.get(i).ownText());
                   historyBeans.add(historyBean);
               }
               listView.setAdapter(historyAdapter);
           }
       }.execute(link);
    }

碎片示例:

public class Folio extends Fragment {
    private ArrayList<FolioBean> folioBeans;
    private TableLayout tableFolio;
    private String guestId;
    private String hotel;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.folio, null);
         guestId = getArguments().getString("guestid");
         hotel = getArguments().getString("hotel");

        folioBeans = new ArrayList<>();
        tableFolio = (TableLayout) v.findViewById(R.id.tableFolio);
        if (InternetResiver.isOnline(getActivity())) {
            setData();
        } else {
            AlertDialog alert = InternetResiver.getAlertDialog(getActivity());
            alert.show();
        }
        return v;
    }

    public static Folio newInstance(String guestid,String hotel,String room) {
        Folio f = new Folio();
        Bundle b = new Bundle();
        b.putString("guestid", guestid);
        b.putString("hotel", hotel);
        b.putString("room", room);
        f.setArguments(b);
        return f;
    }

    public void setData() {
        String link = String.format("htype=xml", guestId, hotel);
        new MyAsincTask(getActivity()){
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(Document document) {
                super.onPostExecute(document);
                Elements elementsDate = document.select("date");
                Elements elementsName = document.select("name");
                Elements elementsSumm = document.select("summa");

                for (int i = 0; i < elementsName.size(); i++) {
                    FolioBean folioBean = new FolioBean();
                    folioBean.setDate(elementsDate.get(i).ownText());
                    folioBean.setName(elementsName.get(i).ownText());
                    folioBean.setSumm(elementsSumm.get(i).ownText());
                    folioBeans.add(folioBean);
                }
                TableRow.LayoutParams row_params = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 20f);
                TableRow.LayoutParams row_params2 = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 30f);

                for (FolioBean folioBean : folioBeans) {
                    TableRow tableRow = new TableRow(App.get());
                    TextView data = new TextView(App.get());
                    TextView name = new TextView(App.get());
                    TextView summ = new TextView(App.get());
                    data.setTextSize(10);
                    name.setTextSize(10);
                    summ.setTextSize(10);
                    name.setTextColor(Color.BLACK);
                    summ.setTextColor(Color.BLACK);
                    data.setTextColor(Color.BLACK);
                    if (folioBean.getName().contains("Проживание")){
                        data.setTypeface(null, Typeface.BOLD);
                        name.setTypeface(null, Typeface.BOLD);
                        summ.setTypeface(null, Typeface.BOLD);
                    }
                    data.setText(" " + folioBean.getDate());
                    name.setText(" " + folioBean.getName() + " ");
                    summ.setText(folioBean.getSumm() + " ");
                    tableRow.addView(data, row_params2);
                    tableRow.addView(name, row_params2);
                    tableRow.addView(summ, row_params);
                    tableFolio.addView(tableRow);
                }
            }
        }.execute(link);
    }
}

1 个答案:

答案 0 :(得分:0)

在onPost()方法中对文档进行检查

if (document!=null){

      Elements elementsId = document.select("id");
           Elements elementsCheckin = document.select("checkin");
           Elements elementsCheckout = document.select("checkout");
           Elements elementsNights = document.select("nights");

           for (int i = 0; i < elementsId.size(); i++) {
               HistoryBean historyBean = new HistoryBean();
               historyBean.setId(elementsId.get(i).ownText());
               historyBean.setCheckin(elementsCheckin.get(i).ownText());
               historyBean.setCheckout(elementsCheckout.get(i).ownText());
               historyBean.setNigth(elementsNights.get(i).ownText());
               historyBeans.add(historyBean);
           }
           listView.setAdapter(historyAdapter);
}