java - onPostExecute 似乎没有被调用

标签 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 中的 UI没有被击中(据我所知)。

这是我的代码

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


    }
}

}       

最佳答案

onPostExecute 的签名错误。应该是这样的

protected void onPostExecute(Void result) {
}

关于java - onPostExecute 似乎没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20636974/

相关文章:

android - android多人棋盘游戏的良好实践

java - Ebean:在 RawSql 中为 setParameter() 设置 List<String>

android - SQLite 数据库在 SSMS 文件夹中不可见

android - flutter 构建失败。 Android 依赖 'androidx.core:core' 具有不同版本的编译(1.0.0)和运行时(1.0.1)类路径

Android:删除数据库中的特定行

android - 在 HttpAsyncTask 中使用从外部类获得的值

java - 从 Activity 中的异步任务获取值(value)

Java自定义像素渲染引擎bug

java - 从多个 sqlite 表填充 Jtable

java - 为什么 gzip 压缩缓冲区大小大于未压缩缓冲区?