似乎没有调用onPostExecute

时间:2013-12-17 14:37:50

标签: java android android-asynctask

当我运行我的应用时,似乎没有调用onPostExecute。它不像应该的那样填充UI。此外,在DoInBackground上,任何通过for循环的日志消息:

for (int i = 0; i < businesses.length(); i++) { }

不显示该特定for循环中的日志消息。因此,例如,第二个for循环for(int j = 0; j < businessNames.size(); j++) { }中的日志消息由于某种原因未显示。这是时间问题还是我错过了什么?

但总而言之,我的onPostExecute中的用户界面没有被击中(据我所知)。

这是我的代码

public class CoffeeResultActivity extends Activity{

ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> businessNames = new ArrayList<String>();
List<String> businessInfo = new ArrayList<String>();
HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>();

private int lastExpandedPosition = -1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    expListView = (ExpandableListView) findViewById(R.id.lvExp);

    //Calling AsyncTask
    new RetreiveSearchResults().execute("coffee");

    // Listview Group click listener
    expListView.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {
            return false;
        }
    });

    expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            if (lastExpandedPosition != -1 && groupPosition != lastExpandedPosition) {
                expListView.collapseGroup(lastExpandedPosition);
            }

            lastExpandedPosition = groupPosition;

        }
    });

    // Listview Group collasped listener
    expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    businessNames.get(groupPosition) + " Collapsed",
                    Toast.LENGTH_SHORT).show();

        }
    });

    // Listview on child click listener
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            // TODO Auto-generated method stub
            Toast.makeText(
                    getApplicationContext(),
                    businessNames.get(groupPosition)
                            + " : "
                            + listDataChild.get(
                                    businessNames.get(groupPosition)).get(
                                    childPosition), Toast.LENGTH_SHORT)
                    .show();
            return false;
        }
    });

}

class RetreiveSearchResults extends AsyncTask<String, Void, Void> {


    @Override
    protected Void doInBackground(String... terms) {        

        // Some example values to pass into the Yelp search service.  
        String category = "coffee";

        // Execute a signed call to the Yelp service.  
        OAuthService service = new ServiceBuilder().provider(YelpApi2.class).apiKey("key").apiSecret("key").build();
        Token accessToken = new Token("key", "key");
        OAuthRequest request = new OAuthRequest(Verb.GET, "http://api.yelp.com/v2/search");
        request.addQuerystringParameter("location", "Waterfront, Boston, MA");
        request.addQuerystringParameter("category", category);
        service.signRequest(accessToken, request);
        Response response = request.send();
        String rawData = response.getBody();



            try {
                JSONObject json = new JSONObject(rawData);
                JSONArray businesses;
                businesses = json.getJSONArray("businesses");

                for (int i = 0; i < businesses.length(); i++) {
                        JSONObject business = businesses.getJSONObject(i);
                        //Log.d("FOO FOO", "FOO FOO FOO" + business.toString());
                        businessNames.add(business.get("name").toString());

                        //The following log message gets displayed
                        Log.d("FOO FOO", "FOO FOO " + businessNames.get(i));

                        businessInfo.add(business.get("rating").toString());

                        //The following log message gets displayed
                        Log.d("FOO FOO", "FOO FOO " + businessInfo.get(i));
                }
                //The following log message gets displayed
                Log.d("FOO FOO", "SIZE" + businessNames.size());
                for(int j = 0; j < businessNames.size(); j++) {

                    //The following log message DOES NOT GET DISPLAYED. But it does run through this for loop in debugger.
                    Log.d("FOO FOO", "FOO FOO ##### Get Here?);

                    //In Debugger, listDataChild does get populated.
                    listDataChild.put(businessNames.get(j), businessInfo);
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   

        return null;
    }

    protected void onPostExecute() {

        //Does not enter onPostExecute in Debugger nor on a Regular run.

        //Log message does NOT get printed
        Log.d("FOO FOO", "FOO FOO Get in POST?");

        listAdapter = new ExpandableListAdapter(CoffeeResultActivity.this, businessNames, listDataChild);
        listAdapter.notifyDataSetChanged();

        // setting list adapter
        expListView.setAdapter(listAdapter);


    }
}

}       

3 个答案:

答案 0 :(得分:5)

onPostExecute的签名是错误的。它应该像

protected void onPostExecute(Void result) {
}

答案 1 :(得分:2)

您的onPostExecute与AsyncTasks签名不匹配。

尝试将Void参数添加到方法中,如下所示:

protected void onPostExecute(Void result){}

答案 2 :(得分:0)

您好像忘记将@Override标记添加到onPostExecute