简体   繁体   English

onPostExecute似乎没有被调用

[英]onPostExecute doesn't seem to get called

When I run my app, onPostExecute doesn't seem to be called. 当我运行我的应用程序时,似乎未调用onPostExecute It is not populated the UI like it should be. 它并未像应有的那样填充UI。 Also, on DoInBackground , any log messages past the for loop: 另外,在DoInBackground ,通过for循环的所有日志消息:

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

excluding the log messages in that particular for loop are not shown. 未显示该特定for循环中的日志消息除外。 So for example, the log message in the 2nd for loop for(int j = 0; j < businessNames.size(); j++) { } are not shown for some reason. 因此,例如,出于某些原因,未显示for for(int j = 0; j < businessNames.size(); j++) { }的第二个for循环中的日志消息。 Is this a timing issue or am I missing something? 这是时间问题还是我错过了什么?

But just to sum up, the UI in my onPostExecute is not being hit (as I know of). 但总而言之,我的onPostExecute的UI没有被击中(据我所知)。

Here is my code 这是我的代码

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);


    }
}

}       

The signature of onPostExecute is wrong. onPostExecute的签名错误。 It should be like 应该像

protected void onPostExecute(Void result) {
}

Your onPostExecute does not match the AsyncTasks signature. 您的onPostExecute与AsyncTasks签名不匹配。

Try adding a Void parameter to the method like so: 尝试将Void参数添加到方法中,如下所示:

protected void onPostExecute(Void result){}

您似乎忘记了将@Override标记添加到onPostExecute

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM