片段中的OnPostExecute方法

时间:2018-08-14 14:53:56

标签: java android android-studio

有人知道我要查找ViewById时如何处理OnPostExecute方法中的错误。我知道无法在Fragments中使用findViewById,但无法在此方法中找到解决方案

公共类TelephoneFragment扩展了片段{

ListView listview;
ListViewAdapter adapter;

public TelephoneFragment() {
    // Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View vs=inflater.inflate(R.layout.fragment_home, container, false);
    // Execute DownloadJSON AsyncTask
    new JsoupListView().execute();
    return vs;


}
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("Android Jsoup ListView Tutorial");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
    }

   ....

... ...

    @Override
    protected void onPostExecute(Void result) {
        // Locate the listview in listview_main.xml
        listview = (ListView)findViewById(R.id.listview);
        // Pass the results into ListViewAdapter.java
        adapter = new ListViewAdapter(getActivity(), arraylist);
        // Set the adapter to the ListView
        listview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();

    }
}

}

1 个答案:

答案 0 :(得分:0)

您不能在所需的片段中使用findViewById。您需要从onCreateView或onViewCreated方法获得根视图,然后才能在其中找到一些视图。 您可以在onCreateView中找到所需的视图,在片段中保留对它们的引用,然后在onPostExecute中使用它们。