调用AsyncTask错误:无法将LoadBookBuyInfo解析为类型

时间:2013-03-29 22:57:02

标签: android callback android-asynctask fragment

在我调用AsyncTask,新的LoadBookBuyInfo()。execute();时,我收到错误:“LoadBookBuyInfo无法解析为类型”。我正在使用片段,我已经在互联网上搜索答案,但无济于事。非常感谢任何帮助,谢谢。

这是我的代码:

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

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

    //bookLV = (ListView)view.findViewById(R.id.list);
    //bookLV = getListView();
    bookLV = (ListView)view.findViewById(android.R.id.list);

    //enables filtering for the contents of the given ListView bookLV
    bookLV.setTextFilterEnabled(true);

    //Search inputed book title
    titleSearch = (EditText)view.findViewById(R.id.titleSearch);


    return view;
}


@Override
public void onActivityCreated(Bundle savedInstanceState){

    System.out.println("onActivityCreated executed");

    bookArray = new ArrayList<Bookinfo>();

    //**************************************************************************************//
    //*******************New Async Task & JSON Parser code starts here**********************//
    //**************************************************************************************//

    //Load bookArray in Background Thread
    new LoadBookBuyInfo().execute();


    //Background AsyncTask to load all BookBuyInfo by making Http Request
    class LoadBookBuyInfo extends AsyncTask<String, String, String> {

        //Show Progress Dialog before starting background thread
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            //pDialog = new ProgressDialog(BuyFragTab); //might can only be activity
            pDialog = new ProgressDialog(getActivity());//might can only be activity
            pDialog.setMessage("Loading Books...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        //Get BookBuyInfo Data
        protected String doInBackground(String... arg0) {
            //Building parameters
            ArrayList<NameValuePair> bookValues = new ArrayList<NameValuePair>();

            //getting JSON string from URL
            JSONObject json = jsonParser.makeHttpRequest(BOOKBUY_URL, "GET", bookValues);

            //Check logcat for JSON response
            Log.d("Book Buy JSON: ", json.toString());

            //Get JSON Data
            try {
                bookBuyInfo = json.getJSONArray("books");

                //bookArray = new ArrayList<Bookinfo>();
                //loop through all books and load data
                for (int i = 0; i < bookBuyInfo.length(); i++) {
                    JSONObject b = bookBuyInfo.getJSONObject(i);
                    ...
                    (book code setup)}
            }
            catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

    //}

        //After completing background task, dismiss the progress dialog
        protected void onPostExecute(String file_url) { // might need to be (String result) here
            //dismiss the dialog after getting all the records
            pDialog.dismiss();
            //update UI from background thread
            //runOnUiThread(new Runnable() {
            getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    //updating parsed JSON data into ListView
                    adapter = new MyCustomAdapter(getActivity(), R.layout.buy_tbfrag, bookArray);
                    bookLV.setAdapter(adapter);
                }
            });
        }
    }

1 个答案:

答案 0 :(得分:1)

看起来您将AsyncTask类放在onActivityCreated方法中。将整个AsyncTask类移到此方法之外,然后从那里调用它。