gpt4 book ai didi

android - 处理 CursorLoader 异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:12:06 25 4
gpt4 key购买 nike

我有一个 Fragment 实现 LoaderManager 并使用 CursorLoader(没什么特别的)。我想捕获查询期间抛出的异常,但我不知道如何处理!!!有什么帮助吗?谢谢。

最佳答案

我尝试继承并实现一个监听器,然后我尝试继承并实现一个回调。就我而言,最简单且侵入性较小的解决方案似乎如下

public class CursorLoaderGraceful extends CursorLoader {
public Throwable error; // holder
public CursorLoaderGraceful(Context context) {
super(context);
}
public CursorLoaderGraceful(Context context, Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
super(context, uri, projection, selection, selectionArgs, sortOrder);
}
public void OnQueryException(RuntimeException throwable) {
throw throwable;
}

@Override
public Cursor loadInBackground() {
try {
return (super.loadInBackground());
} catch (Throwable t) {
error = t; // keep it
}
return (null);
}
}

在 fragment/Activity 中

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoaderGraceful loader = new CursorLoaderGraceful(this., other, params, go , here);
// ...
return loader;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
//trivial code
mAdapter.swapCursor(data);
if (this.isResumed()) {
this.setListShown(true);
} else {
this.setListShownNoAnimation(true);
}

//check and use
Throwable loaderError = ((CursorLoaderGraceful)loader).error;
if (loaderError != null) {
//all these just to show it?!?!? :/
Toast.makeText(this, loaderError.getMessage(), Toast.LENGTH_SHORT)
.show();
}
}

关于android - 处理 CursorLoader 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13551219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com