0

在调用我的 AsyncTask 时,new 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);
                }
            });
        }
    }
4

1 回答 1

1

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

于 2013-03-29T23:01:09.233 回答