跳过了 147 帧!应用程序可能在其主线程上做了太多工作 [英] skipped 147 frames! The application may be doing too much work on its main thread

查看:14
本文介绍了跳过了 147 帧!应用程序可能在其主线程上做了太多工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解这个错误的含义.我在 stackoverflow.com 上发现了许多类似的问题,我已经尝试实现建议的答案,但仍然出现此错误.我想要做的是使用 php web 服务我从 mysql 数据库服务器中提取数据并尝试使用 AsyncTask 在列表视图中显示它,如下所示:

I understand the meaning of this error. I found many similar questions here at stackoverflow.com and I have tried to implement the answers those were suggested but still I am getting this error. What I am trying to do is using php web service I am extracting the data from mysql database server and trying to display it in listview using AsyncTask as follows:

class LoadAllProducts extends AsyncTask<String, String, ArrayList<HashMap<String, String>>> 
{
    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
    protected ArrayList<HashMap<String, String>> doInBackground(String... args)
    {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

        Log.d("All Products: ", json.toString());
        try {

                JSONArray  files = json.getJSONArray(TAG_FILES);
                for(int i=0;i<files.length();i++)
                {                     
                    HashMap<String, String> map = new HashMap<String, String>();    
                    JSONObject e = files.getJSONObject(i);


                    String file_name = e.getString(TAG_FILE_NAME);
                    String sender = e.getString(TAG_SENDER);
                    String subject = e.getString(TAG_SUBJECT);


                    map.put(TAG_SENDER, sender);
                    map.put(TAG_SUBJECT, subject);

                    mylist.add(map);            
                } 


        } catch (JSONException e) 
        {
            e.printStackTrace();
            Log.e("log_tag", "Error parsing data "+e.toString());
        }

        return mylist;

    }

许多答案都建议所有处理都应在 doInBackground 函数中完成.现在下面是在 ListView 中显示这个数组列表的代码

This was suggested in many answers that all the processing should be done in doInBackground function. Now below is the code to display this arraylist in ListView

 protected void onPostExecute(String file_url) 
    {

        pDialog.dismiss();

        runOnUiThread(new Runnable() 
        {
            public void run() 
            {
                String[] from = { TAG_SENDER, TAG_SUBJECT };
                int[] to = { android.R.id.text1, android.R.id.text2 };


                ListAdapter adapter = new SimpleAdapter(AllProductsActivity.this, mylist,
                        android.R.layout.simple_list_item_2, from , to);
                      setListAdapter(adapter);

            }
        });

    }

请帮助,因为首先我是android的初学者,我不知道如何解决这个问题.请检查我的代码并告诉我问题.

Please Help cause first of all I am a beginner of android and I dont have any clue how to solve this problem . Please check my code and let me know the problem.

推荐答案

如果你的主线程做了这么多工作,就会出现这种类型的错误.基本上,当我们在模拟器中测试我们的应用程序时,跳帧会发生最长时间,因为它已经很慢并且无法像设备一样处理大量操作.

This type of error will come if your main thread doing so much work. Basically skip frames will happen maximum time when we test our app in emulator, because its already slow and unable to handle huge operation like a device.

我在您的代码中看到了一个简单的问题.我们知道 onPostExecute() 在 MainThread 上运行,但您仍然在 onPostExecute() 中使用 runOnUiThread(new Runnable() .

I saw a simple problem in your code. We know onPostExecute() runs on MainThread, But still you use runOnUiThread(new Runnable() in onPostExecute() .

你 doinbackground 方法返回一个 ArrayList ,但你的 onpostexecute 持有一个字符串..

You doinbackground method returns an ArrayList , but your onpostexecute hold a string..

将其更改为 my onPostExecute' 参数,即您在适配器中使用的 ArrayList(mylist).

Change it as my onPostExecute' parameter that is the ArrayList(mylist)you use in your adapter.

编辑

  class LoadAllProducts extends AsyncTask<String, String, ArrayList<HashMap<String, String>>> {

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
    protected ArrayList<HashMap<String, String>> doInBackground(String... args){
    HttpClient client = new DefaultHttpClient();
    HttpGet getData = new HttpGet("your_url");
    HttpResponse response = client.execute(getData);
    BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    String data = "";

    while((data = br.readLine()) != null){
    JsonArray arr = new JsonArray(data);
       for(int i=0;i<arr.length();i++)
            {                     
                HashMap<String, String> map = new HashMap<String, String>();    
                JSONObject e = arr.getJSONObject(i);

                String file_name = e.getString(TAG_FILE_NAME);
                String sender = e.getString(TAG_SENDER);
                String subject = e.getString(TAG_SUBJECT);


                map.put(TAG_SENDER, sender);
                map.put(TAG_SUBJECT, subject);

                mylist.add(map);            

    }

    return mylist;

}

 protected void onPostExecute(ArrayList<HashMap<String, String>> mylist) {
            String[] from = { TAG_SENDER, TAG_SUBJECT };
            int[] to = { android.R.id.text1, android.R.id.text2 };


            ListAdapter adapter = new SimpleAdapter(AllProductsActivity.this, mylist,
                    android.R.layout.simple_list_item_2, from , to);
                  setListAdapter(adapter);

              pDialog.dismiss();

        }

这篇关于跳过了 147 帧!应用程序可能在其主线程上做了太多工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆