MainFragment BaseAdapter错误

时间:2016-02-02 15:37:21

标签: android android-asynctask

MainFragment:

public class MainFragment extends Fragment {

    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    static String RANK = "rank";
    static String COUNTRY = "country";
    static String POPULATION = "population";
    static String FLAG = "flag";
    // URL Address
    String url = "http://";

    public MainFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view =inflater.inflate(R.layout.activity_main, container, false);

        new JsoupListView().execute();
        return view;
    }

    // Title AsyncTask
    private class JsoupListView extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(getActivity());
            // Set progressdialog title
            mProgressDialog.setTitle("Diziler Yükleniyor");
            // Set progressdialog message
            mProgressDialog.setMessage("Yükleniyor...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();

            try {
                // Connect to the Website URL
                Document doc = Jsoup.connect(url).get();
                // Identify Table Class "worldpopulation"
                for (Element table : doc.select("div[class=col-sm-12 col-xs-12 pad0 middle]")) {

                    // Identify all the table row's(tr)
                    for (Element row : table.select("div[class=col-sm-12 col-xs-12 pad0 streamingBoxWrap mNewsItem]:gt(0)")) {
                        HashMap<String, String> map = new HashMap<String, String>();

                        // Identify all the table cell's(td)
                        Elements tds = row.select("a");

                        // Identify all img src's
                        Elements imgSrc = row.select("img[src]");
                        // Get only src from img src
                        String imgSrcStr = imgSrc.attr("src");

                        Elements aSrc = row.select("a[href]:gt(1)");
                        String aSrcStr = aSrc.attr("href");

                        // Retrive Jsoup Elements
                        // Get the first td
                        map.put("rank", aSrcStr);
                        // Get the second td
                        map.put("country", tds.get(1).text());
                        // Get the third td
                        map.put("population", tds.get(2).text());
                        // Get the image src links
                        map.put("flag", imgSrcStr);
                        // Set all extracted Jsoup Elements into the array
                        arraylist.add(map);
                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // Locate the listview in listview_main.xml
            listview = (ListView) getActivity().findViewById(R.id.listview);
            // Pass the results into ListViewAdapter.java

            adapter = new ListViewAdapter(getActivity(), arraylist);
            listview.setAdapter(adapter);



            // Close the progressdialog
            mProgressDialog.dismiss();
        }
    }
}

ListViewAdapter:

public class ListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    ImageLoader imageLoader;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public ListViewAdapter(Context context,
                           ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        imageLoader = new ImageLoader(context);
    }



    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables
        TextView rank;
        TextView country;
        TextView population;
        ImageView flag;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.singleitemview, parent, false);
        // Get the position
        resultp = data.get(position);

        // Locate the TextViews in listview_item.xml
        rank = (TextView) itemView.findViewById(R.id.rank);
        country = (TextView) itemView.findViewById(R.id.country);
        population = (TextView) itemView.findViewById(R.id.population);

        // Locate the ImageView in listview_item.xml
        flag = (ImageView) itemView.findViewById(R.id.flag);

        // Capture position and set results to the TextViews
        rank.setText(resultp.get(MainFragment.RANK));
        country.setText(resultp.get(MainFragment.COUNTRY));
        population.setText(resultp.get(MainFragment.POPULATION));
        // Capture position and set results to the ImageView
        // Passes flag images URL into ImageLoader.class
        imageLoader.DisplayImage(resultp.get(MainFragment.FLAG), flag);
        // Capture ListView item click

        return itemView;
    }
}

错误:

  

在   omer.s.MainFragment $ JsoupListView.onPostExecute(MainFragment.java:125)   在   omer.s.MainFragment $ JsoupListView.onPostExecute(MainFragment.java:55)

MainFragment.java:125:

listview.setAdapter(adapter);

MainFragment.java:55:

    private class JsoupListView extends AsyncTask<Void, Void, Void> {

1 个答案:

答案 0 :(得分:0)

问题是您正在查看主机ListView布局中的Activity,但您可能想要ListView布局中的Fragment

onCreateView(...)方法更新为:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {

    View view =inflater.inflate(R.layout.activity_main, container, false);
    listview = (ListView) view.findViewById(R.id.listview);

    new JsoupListView().execute();
    return view;
}

然后从listview = (ListView) getActivity().findViewById(R.id.listview);移除onPostExecute(),因为您已经引用了所需的listview