跳过147帧!该应用程序可能会做太多的工作,它的主线程它的、太多、主线、会做

2023-09-07 01:26:20 作者:筱川奈奈子

我理解这个错误的含义。我发现有很多类似的问题在这里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函数来完成。现在,下方则是code,显示此ArrayList中的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的初学者,我没有任何线索如何解决这个问题。请检查我的code,让我知道这个问题。

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.

我在你的code看到了一个简单的问题。我们知道onPostExecute()运行在MainThread,但你仍然使用 runOnUiThread(新的Runnable()在onPostExecute()。

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..

更改它作为我的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();

        }