2017-08-08 54 views
0

的工作太多了,我有兩個班。一個是我的SettingsActivity,另一個是我的volley。在我的Web服務中,它有很多數據,需要將其轉換爲ArrayList,並將其保存到我的Database;我有一個兩個Web服務要在第一個完成之前調用,在第二個啓動之前它會延遲1秒。這些東西在它之後我Log它總是顯示以下如何避免應用程序可以做它的主線程

I/Choreographer: Skipped 226 frames! The application may be doing too much work on its main thread. 
V/RenderScript: 0x5595d9d0e0 Launching thread(s), CPUs 8 
W/art: Suspending all threads took: 20.500ms 
W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/data/com.app.myapp/databases/myDB.sqlite' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed. 
I/art: Background partial concurrent mark sweep GC freed 38249(2025KB) AllocSpace objects, 2(968KB) LOS objects, 40% free, 16MB/26MB, paused 569us total 130.628ms 
I/Choreographer: Skipped 1528 frames! The application may be doing too much work on its main thread. 

我已經檢查我的數據庫是使用和所有的關閉後關閉。但我仍然有database is leaked消息和Skipped xxx frames。我已經嘗試創建一個新的線程,我調用了class時調用我的課,我SettingsActivityVolley

btnSync.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View v) { 

    Thread thread = new Thread(new Runnable(){ 
       @Override 
       public void run(){ 
        Parameter.parameterOneVolley(SettingsActivity.this, getApplicationContext()); 
       } 
      }); 
    } 
    }); 

試圖在這之後我遇到Can't create handler inside thread that has not called Looper.prepare()

然後也用這個

runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
        Parameter.parameterOneVolley(SettingsActivity.this, getApplicationContext()); 
       } 
      }); 
嘗試

但是,這我有一個消息Skipped frames。如何解決它?提前感謝您的幫助。

在我Paramater類我還呼籲ProgressDialog

這裏是我的代碼爲Paramter.parameterOneVolley

public static void parameterOneVolley(final Activity activity, final Context context) { 

    final ProgressDialog pd = new ProgressDialog(activity); 
    pd.setMessage("Fetching data...."); 
    pd.show(); 

    initializeDatabase(context); 
    sqLiteAdapter = new SQLiteAdapter(activity); 

    regionList = new ArrayList<Region>(); 
    divisionList = new ArrayList<Division>(); 


    final Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 

      try { 

       JSONArray regionArr = response.getJSONArray("Region"); 
       JSONArray divisionArr = response.getJSONArray("Division"); 

       sqLiteAdapter.openToRead(); 



        for(int i = 0; i < regionArr.length(); i++){ 
         JSONObject regionObj = (JSONObject) regionArr.get(i); 
         Region region = new Region(regionObj.getInt("RegionId"),regionObj.getString("Region")); 

         regionList.add(region); 


        if(regionList.size()!=0) { 
         sqLiteAdapter.insertOrReplaceRegion(regionList); 
        } 
       } 


        for(int i = 0; i < divisionArr.length(); i++){ 
         JSONObject divisionObj = (JSONObject) divisionArr.get(i); 
         Division division = new Division(divisionObj.getInt("RegionId"),divisionObj.getInt("DivisionId"),divisionObj.getString("Division")); 

         divisionList.add(division); 


        if(divisionList.size()!=0) { sqLiteAdapter.insertOrReplaceDivision(divisionList); 
        } 
       } 

       sqLiteAdapter.close(); 

       if(pd != null && pd.isShowing()) { 
        pd.dismiss(); 
       } 

      } catch (JSONException e) { 
       e.printStackTrace(); 
       sqLiteAdapter.close(); 

       if(pd != null && pd.isShowing()) { 
        pd.dismiss(); 
       } 

       Toast.makeText(context, 
         e.getMessage(), Toast.LENGTH_SHORT).show(); 
      } 


      Toast.makeText(context, "Successfully synced.", Toast.LENGTH_SHORT).show(); 
        //call next web service 
        final Handler handler = new Handler(); 
        handler.postDelayed(new Runnable() { 
         @Override 
         public void run() { 
          parameterTwoVolley(activity, context); 
        } 
        }, 1000); 

     } 
    }; 

    final Response.ErrorListener errorListener = new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      if(pd != null && pd.isShowing()) { 
        pd.dismiss(); 
       } 

      if (error.networkResponse != null) { 
       Log.d(TAG, "Error Response code: " + error.networkResponse.statusCode); 
       Toast.makeText(context, error.networkResponse.statusCode, Toast.LENGTH_SHORT).show(); 
      } 
      if (error instanceof TimeoutError || error instanceof NoConnectionError) { 
       Log.d(TAG, "Error Response code: Timeout/NoConnection"); 
       Toast.makeText(context, "Timeout/NoConnection", Toast.LENGTH_SHORT).show(); 
      } else if (error instanceof AuthFailureError) { 
       Log.d(TAG, "Error Response code: AuthFailureError"); 
       Toast.makeText(context, "AuthFailureError", Toast.LENGTH_SHORT).show(); 
      } else if (error instanceof ServerError) { 
       Log.d(TAG, "Error Response code: ServerError"); 
       Toast.makeText(context, "ServerError", Toast.LENGTH_SHORT).show(); 
      } else if (error instanceof NetworkError) { 
       Log.d(TAG, "Error Response code: NetworkError"); 
       Toast.makeText(context, "NetworkError", Toast.LENGTH_SHORT).show(); 
      } else if (error instanceof ParseError) { 
       Log.d(TAG, "Error Response code: ParseError"); 
       Toast.makeText(context, "ParseError", Toast.LENGTH_SHORT).show(); 
      } 

      if(pd != null && pd.isShowing()) { 
       pd.dismiss(); 
      } 
     } 
    }; 
} 

我嘗試添加

activity.runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
        pd = new ProgressDialog(activity); 
      pd.setMessage("Fetching data...."); 
      pd.show(); 
     } 
       } 
      }); 

activity.runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
        if(pd != null && pd.isShowing()) { 
         pd.dismiss(); 
        } 
        Toast.makeText(context, "Successfully synced.", Toast.LENGTH_SHORT).show(); 
       } 
      }); 
每次

我使用Progress Dialog and Toast但我仍然遇到Skipped frames

使用ProgressBar還試圖代替並顯示並隱藏它但仍然遇到skipped frames

+2

取決於你在做什麼'Parameter.parameterOneVolley'方法。在'runOnUiThread'中只包裹UI相關的部分 –

回答

1

在Android中,只有主線程(也稱爲UI線程)可以更新的意見。這是因爲在Android中,UI工具包不是線程安全的。

當您嘗試從一個工作線程的Android更新UI引發此異常。如果您想從另一個線程使用處理程序更新UI。

final Handler handler=new Handler(); 
new Thread(new Runnable() { 
    @Override 
    public void run() { 
     //your code 
     handler.post(new Runnable() { 
      @Override 
      public void run() { 
       Parameter.parameterOneVolley(SettingsActivity.this, getApplicationContext()); 
      } 
     }); 
    } 
}).start(); 
+0

爲了使這個有用的答案,你想添加更多的解釋... – GhostCat

+0

我應該把它放在哪裏?在我的設置活動? – natsumiyu

+0

看看更新後的回答 –

相關問題