我的应用闲置一段时间后变现

时间:2018-08-07 06:50:02

标签: android listview android-asynctask

我有一个显示事件列表的应用程序。该列表的数据是从api收集的,我使用asynctask来获取此数据。一切正常,但由于某些原因,应用程序在长时间的空闲(几个小时)后崩溃。在显示何时重新输入的片段中,应用程序包含徽标和listview。徽标正在显示,但是listview消失了,并且在应用崩溃后不久。 问题是我无法通过调试检查错误。由于某种原因,在调试模式下不会发生该错误。

关于可能导致崩溃的任何想法? 在您设置重复项之前,我已经检查了另一篇文章,但是他们的问题与我的不同。

我使用的 scrollDisabledListview

public class ScrollDisabledListView extends ListView {

private int mPosition;

public ScrollDisabledListView(Context context) {
    super(context);
}

public ScrollDisabledListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public ScrollDisabledListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;

    if (actionMasked == MotionEvent.ACTION_DOWN) {
        // Record the position the list the touch landed on
        mPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
        return super.dispatchTouchEvent(ev);
    }

    if (actionMasked == MotionEvent.ACTION_MOVE) {
        // Ignore move events
        return true;
    }

    if (actionMasked == MotionEvent.ACTION_UP) {
        // Check if we are still within the same view
        if (pointToPosition((int) ev.getX(), (int) ev.getY()) == mPosition) {
            super.dispatchTouchEvent(ev);
        } else {
            // Clear pressed state, cancel the action
            setPressed(false);
            invalidate();
            return true;
        }
    }

    return super.dispatchTouchEvent(ev);
 }
}

异步任务:

private static class loginTask extends AsyncTask<Void, Void, Boolean>{
    private WeakReference<RingbillettFragmentActivity> activityWeakReference;

    loginTask(RingbillettFragmentActivity activity){
        activityWeakReference = new WeakReference<RingbillettFragmentActivity>(activity);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        RingbillettFragmentActivity activity = activityWeakReference.get();

        if(activity == null || activity.isFinishing()){
            return;
        }

    }

    @Override
    protected Boolean doInBackground(Void... params) {

        ArrayList<NameValuePair> myParams;
        JSONObject myJSON = null;
        boolean checkLogin = true;
        RingbillettFragmentActivity activity = activityWeakReference.get();
        if(activity == null || activity.isFinishing()){
            return false;
        }

        try {

            myParams = new ArrayList<NameValuePair>();
            myParams.add(new BasicNameValuePair("token", RingbillettUtils.hentToken(activity.getApplicationContext())));

            myJSON = RingbillettUtils.getJSONfromWebservice("validateToken", myParams);

            if (myJSON.getBoolean("TokenValid")) {
                activity.doLog("TokenValid = true");
                checkLogin = true;
            } else {

                activity.doLog("TokenValid = false");

                String OS = "Android";
                Integer OS_ver = android.os.Build.VERSION.SDK_INT;

                myParams.add(new BasicNameValuePair("b", RingbillettUtils.hentLagretBrukernavn(activity.getApplicationContext())));
                myParams.add(new BasicNameValuePair("p", RingbillettUtils.hentLagretPassord(activity.getApplicationContext())));
                myParams.add(new BasicNameValuePair("OS", OS));
                myParams.add(new BasicNameValuePair("OS_ver", Integer.toString(OS_ver)));

                myJSON = RingbillettUtils.getJSONfromWebservice("DoLogin", myParams);

                if (myJSON.getBoolean("godkjent")) {

                    activity.doLog("DoLogin = godkjent");

                    String nyToken = myJSON.getString("token");
                    RingbillettUtils.lagreToken(activity.getApplicationContext(), nyToken);

                    checkLogin = true;
                } else {

                    activity.doLog("DoLogin = IKKE godkjent");

                    RingbillettUtils.lagreToken(activity.getApplicationContext(), null);
                    checkLogin = false;
                }

            }

        } catch (Exception ex) {
            activity.doLog("Exception : " + ex.getMessage());
            checkLogin = false;
        }

        return checkLogin;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        RingbillettFragmentActivity activity = activityWeakReference.get();
        if(activity == null || activity.isFinishing()){
            return;
        }

        if (result) {
            activity.doLog("BRUKER INNLOGGET");

        } else {
            activity.doLog("BRUKER IKKE INNLOGGET");
            RingbillettUtils.lagreToken(activity.context, null);
            RingbillettUtils.lagrePassord(activity.context, null);
            RingbillettUtils.lagreHuskPassord(activity.context, false);
            activity.brukernavn = RingbillettUtils.ANONYM;
            activity.brukerInnlogget = false;


        }

    }

}

以编程方式设置列表视图的高度

if(adapter != null){
        int totalHeight = 0;
        for (int i = 0; i < adapter.getCount(); i++) {
            View listItem = adapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();

    }

0 个答案:

没有答案