android - 我的应用程序在闲置一段时间后兑现
问题描述
我有一个显示事件列表的应用程序。列表的数据是从 a 收集的api
,我使用 aasynctask
来获取这些数据。一切正常,但由于某种原因,应用程序在长时间空闲(几个小时)后崩溃。在我重新进入应用程序时显示的片段中包含一个徽标和一个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();
}
解决方案
推荐阅读
- python - 创建连续 html 解析的最佳方法是什么?
- javascript - 页面加载后,Javascript / Jquery提交没有ID或类的表单
- java - 如何从 selenium webdriver 中的椭圆中获取数据
- mongodb - 从 MongoDB 中的嵌套文档中提取唯一值
- php - 发送 Firebase 推送通知
- java - 在 Springfox Swagger UI 中隐藏 Client_id - 2.8.0
- c# - 如何解决 GDI+ 中的一般错误和内存不足
- ios - CloudKit 区域删除和娱乐
- python - 如何从嵌套字典中查找列表组合
- go - gob.Register 名称未为另一个包中的接口注册