0
嚴格的模式,運行時,當手機變革的方向,我的應用程序將與錯誤遊標未封閉的方向變化
資源在連接的堆棧跟蹤獲取,但從來沒有公佈過崩潰。 有關避免資源泄漏的信息,請參閱java.io.Closeable。 java.lang.Throwable中:顯式的終止方法「關閉」不叫
奇怪的是,如果我以調試模式運行應用程序,並把斷點在所有cursor.close()只是爲了確保它實際上是命中,所有的斷點都會被擊中,應用程序永遠不會崩潰。
這是我的片段,它調用另一個包含所有SqlBrite查詢和遊標的類(爲簡潔起見,代碼已縮短)。
public class MenuSummary extends Fragment{
private DbHelper dbHelper;
private Observable<?> income1, income2;
private Observable imin;
private Subscription s;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbHelper = new DbHelper(getContext());
}
@Override
public void onPause(){
super.onPause();
s.unsubscribe();
}
@Override
public void onResume(){
super.onResume();
displaySummary(selectedYear,selectedMonth);
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
}
private void displaySummary(final int selectedYear, final int selectedMonth){
income1 = dbHelper.getIncome(selectedYear,selectedMonth,1);
income2 = dbHelper.getIncome(selectedYear,selectedMonth,2);
List<Observable<?>> myObservables = Arrays.asList(income1,income2);
imin = Observable.combineLatest(myObservables, new FuncN<List<BigDecimal>>() {
@Override
public List<BigDecimal> call(Object... args) {
List<BigDecimal> listIncome = new ArrayList<~>();
listIncome.add((BigDecimal) args[0]);
listIncome.add((BigDecimal) args[1]);
return listIncome ;
}
}
).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
s = imin.subscribe(new Action1<List<BigDecimal>>() {
@Override
public void call(List<BigDecimal> expensesAndBalance) {
//do plenty of view.settext here
}});
}
這裏是如何,包含了函數getIncome看起來像DbHelper類:
任何人都可以找出哪裏我錯在哪裏?
public class DbHelper {
private MySQLiteHelper mySQLiteHelper;
SqlBrite sqlBrite = new SqlBrite.Builder().build();
BriteDatabase briteDb;
Subscription subscription;
private static DbHelper instance;
public DbHelper(Context context) {
mySQLiteHelper = new MySQLiteHelper(context);
sqlBrite = new SqlBrite.Builder().build();
briteDb = sqlBrite.wrapDatabaseHelper(mySQLiteHelper,Schedulers.io());
}
public Observable<BigDecimal> getIncome(final int endYear, final int endMonth, final int moneyJar){
final String[] args = new String[]{moneyJar + "", endYear + "", endMonth + "", endYear + "", };
Observable<BigDecimal> myObservable;
myObservable = briteDb.createQuery(MySQLiteHelper.TABLE_INCOME, "SELECT total FROM " + MySQLiteHelper.TABLE_INCOME +
" WHERE moneyJar = ? AND ((year = ? AND month <= ?) OR (year < ?)) and isDeleted = 0", args)
.map(new Func1<SqlBrite.Query, BigDecimal>() {
@Override
public BigDecimal call(SqlBrite.Query query) {
Cursor cursor = query.run();
BigDecimal income, incomeTotal = new BigDecimal(0);
if (cursor != null) {
try {
if (cursor.getCount() > 0 && cursor.moveToFirst()) {
do {
income = new BigDecimal(cursor.getString(cursor.getColumnIndex("total")));
incomeTotal = incomeTotal.add(income);
} while (cursor.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
cursor.close();
}
}
return incomeTotal;
}
});
return myObservable;
}
}
我可以發誓我之前試過這個。謝謝! – imin
如果你有時間,你可以看看我的其他問題在這裏http://stackoverflow.com/questions/43019258/pthread-create-failed-couldnt-allocate-1064960-byte-stack-out-of-memory ?謝謝 – imin
@imin,我已經。沒有線索,對不起。如果你在github上發佈一個基本項目,請看看。 – azizbekian