Android UI被AsyncTask阻止并偶尔崩溃

时间:2016-02-05 13:05:14

标签: android android-fragments android-asynctask android-viewpager android-tablayout

MainFragment

它们都是[ (int) 0 => [ 'tag' => 'noombrina', 'count' => (int) 34, 'size' => (float) 60 ], (int) 1 => [ 'tag' => 'comunedilanciano', 'count' => (int) 22, 'size' => (float) 33 ], (int) 2 => [ 'tag' => 'lucianodalfonso', 'count' => (int) 21, 'size' => (float) 30 ], (int) 3 => [ 'tag' => 'comunali2016', 'count' => (int) 20, 'size' => (float) 28 ], (int) 4 => [ 'tag' => 'regioneabruzzo', 'count' => (int) 19, 'size' => (float) 26 ], (int) 5 => [ 'tag' => 'forzaitalia', 'count' => (int) 19, 'size' => (float) 26 ], (int) 6 => [ 'tag' => 'maurofebbo', 'count' => (int) 17, 'size' => (float) 21 ], (int) 7 => [ 'tag' => 'mariopupillo', 'count' => (int) 17, 'size' => (float) 21 ], (int) 8 => [ 'tag' => 'carabinieri', 'count' => (int) 13, 'size' => (float) 12 ], (int) 9 => [ 'tag' => 'toniapaolucci', 'count' => (int) 13, 'size' => (float) 12 ] ] TabLayout的嵌套片段。每个片段里面都有ViewPager所以当我调用这个片段时它们会一起调用,现在当我用其他片段替换这个片段或者当我从其他片段调用这个片段时,我遇到了一个缓慢处理app的问题活动。即便有时我的应用程序进入后台没有任何对话和Android手机的主屏幕显示。

此TabLayout下的每个片段的代码如下:

ListView
从服务获取数据并添加到public class IndicesFragment extends android.support.v4.app.Fragment { public static String imagepath = null; static ArrayList<EquityDetails> catListDao = new ArrayList<EquityDetails>(); static ArrayList<EquityDetails> catListDao1 = new ArrayList<EquityDetails>(); static int count = 0; static int count1 = 0; ListView list; ImageView progressBar; View view; Activity act; AdvisorsAdapter adapter; TextView empty_text; private boolean isViewShown = false; AnimatorSet set; FetchAllData myTask; public static IndicesFragment newInstance() { return new IndicesFragment(); } @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser) { isViewShown = true; if (adapter != null) { adapter.filter(""); } } else { isViewShown = false; } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.equity_activity, container, false); act = this.getActivity(); Constants.check_fragment_visible = 1; count++; return view; } public void onActivityCreated(Bundle savedInstanceState1) { super.onActivityCreated(savedInstanceState1); setHasOptionsMenu(true); list = (ListView) view.findViewById(R.id.list_equity); empty_text = (TextView) view.findViewById(R.id.empty); progressBar = (ImageView) view.findViewById(R.id.progressBar); set = (AnimatorSet) AnimatorInflater.loadAnimator(getActivity(), R.animator.fadein); set.setTarget(progressBar); progressBar.setVisibility(View.GONE); if (Utils.isNetworkAvailable(getActivity())) { if (catListDao.size() > 0) { adapter = new AdvisorsAdapter(act, R.layout.custom_equity, catListDao, 0); list.setAdapter(adapter); } else { if (!isViewShown) { new FetchAllData(getActivity(), 3).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } } } else { CustomToast toast = new CustomToast(getActivity(), "There is no internet connection!"); } } @Override public void onResume() { super.onResume(); Constants.check_fragment_visible = 1; if (Constants.check_reload) { if (Utils.isNetworkAvailable(getActivity())) { new FetchAllData(getActivity(), 3).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } else { CustomToast toast = new CustomToast(getActivity(), "There is no internet connection!"); } } if (adapter != null) adapter.notifyDataSetChanged(); }

FetchdataClass如下:

ListView

public class FetchAllData extends AsyncTask<Void, Void, String> { ProgressDialog pDialog; int typeId; private Context cont; public FetchAllData(Context con, int typeId) { // TODO Auto-generated constructor stub this.cont = con; this.typeId = typeId; Log.d("Constructor Called", "yes"); } @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); progressBar.setVisibility(View.VISIBLE); } @Override protected String doInBackground(Void... params) { // TODO Auto-generated method stub return getString(); } private String getString() { // TODO Auto-generated method stub URL obj = null; HttpURLConnection con = null; try { obj = new URL(Constants.AppBaseUrl + "/call_listing/" + typeId); String userPassword = "oi" + ":" + "kl"; String header = "Basic " + new String(android.util.Base64.encode(userPassword.getBytes(), android.util.Base64.NO_WRAP)); con = (HttpURLConnection) obj.openConnection(); con.addRequestProperty("Authorization", header); con.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); con.setRequestMethod("POST"); // For POST only - BEGIN con.setDoOutput(true); OutputStream os = con.getOutputStream(); os.flush(); os.close(); // For POST only - END int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { //success BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); Log.i("TAG", response.toString()); return response.toString(); } else { Log.i("TAG", "POST request did not work."); } } catch (IOException e) { e.printStackTrace(); } if (con != null) { con.disconnect(); } return null; } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub super.onPostExecute(result); if (result != null) { // pDialog.dismiss(); progressBar.setVisibility(View.GONE); JSONObject jsonObject; try { catListDao = new ArrayList<EquityDetails>(); jsonObject = new JSONObject(result); JSONArray jsonArray = jsonObject.getJSONArray("list"); Log.d("Length ", "" + jsonArray.length()); for (int i = 0; i < jsonArray.length(); i++) { EquityDetails allDirectory = new EquityDetails(); allDirectory.setEntry_value(jsonArray.getJSONObject(i).getString("entry")); allDirectory.setSerial_value(jsonArray.getJSONObject(i).getString("sl")); allDirectory.setTg_value1(jsonArray.getJSONObject(i).getString("tgt_1")); allDirectory.setTg_value2(jsonArray.getJSONObject(i).getString("tgt_2")); allDirectory.setMainTitle_value(jsonArray.getJSONObject(i).getString("script")); allDirectory.setMain_subTitle_value(jsonArray.getJSONObject(i).getString("exchange")); allDirectory.setRating_value(jsonArray.getJSONObject(i).getString("rating")); allDirectory.setReview_value(jsonArray.getJSONObject(i).getString("review")); allDirectory.setImage1(jsonArray.getJSONObject(i).getString("advisor_image")); allDirectory.setPosted_by(jsonArray.getJSONObject(i).getString("posted_by")); allDirectory.setImage2(jsonArray.getJSONObject(i).getString("script_image")); allDirectory.setCall_id(jsonArray.getJSONObject(i).getString("call_id")); allDirectory.setBuy(jsonArray.getJSONObject(i).getString("buy_sentiment")); allDirectory.setSell(jsonArray.getJSONObject(i).getString("sell_sentiment")); allDirectory.setRecommend(jsonArray.getJSONObject(i).getString("recommendation")); allDirectory.setPosted_date(jsonArray.getJSONObject(i).getString("posted_date")); allDirectory.setExpiry_date(jsonArray.getJSONObject(i).getString("expiry_date")); catListDao.add(allDirectory); } catListDao1 = catListDao; ab = true; adapter = new AdvisorsAdapter(act, R.layout.custom_equity, catListDao, 0); list.setAdapter(adapter); } catch (JSONException e) { e.printStackTrace(); } } //pDialog.dismiss(); progressBar.setVisibility(View.GONE); } } 可见时,我正在使用AsyncTask.THREAD_POOL_EXECUTOR在后​​台调用webservice。

当应用程序关闭而没有任何弹出窗口时,我的Stacktrace就像:

Fragment

2 个答案:

答案 0 :(得分:0)

您是否在“onPostExecute”方法中解析您正在解析的数据不是太大。请注意,“onPostExecute”在主线程上执行,因此如果您正在解析的数据集很大,则会导致无响应的行为。

我建议您将数据解析代码移动到“doInBackground”,并仅在“onPostExecute”中设置适配器。这可能会有所帮助。

答案 1 :(得分:0)

我还注意到你正在片段的“onActivityCreated”方法上启动网络操作。

我建议您在“onCreateView”中执行此操作。因此,您只在创建视图时启动下载,而不是在创建活动时启动。

我也注意到你的日志明确说明了 “跳过58帧!应用程序可能在其主线程上做了太多工作。”

这表明在主线程上实际进行了太多的处理。