我遇到以下问题:
java.lang.IllegalStateException:必须在主线程上调用initLoader
当我致电realizarConsulta
时,正好initLoader
方法。
问题是,在阅读条形码后,我想对数据库进行查询
知道条形码是否已经存在。但是当我在下面的代码中尝试它时会抛出IllegalStateException
,我认为将操作移动到EscaneoActivity
类中的成员方法将解决问题,但事实并非如此。
public class EscaneoActivity extends AppCompatActivity implements IPostLoaderConsulta {
SurfaceView camara;
private static Barcode codigoActual;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTitle("Escaner");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scaneo);
camara = (SurfaceView) findViewById(R.id.sv_escaneo_camara);
configurarCamara();
}
public void realizarConsulta(Bundle b){
getSupportLoaderManager().initLoader(MainActivity.LOADER_CONSULTOR_PRODUCTOS_DB, //Lanza java.lang.IllegalStateException: initLoader must be called on the main thread
b,
AdminSingletons.darInstanciaConsultorProductos(this, EscaneoActivity.this));
}
public void configurarCamara() {
BarcodeDetector detector = new BarcodeDetector.Builder(this).build();
final CameraSource cameraSource = new CameraSource.Builder(this, detector)
.setAutoFocusEnabled(true)
.setRequestedPreviewSize(1600, 1024)
.build();
camara.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
if (ActivityCompat.checkSelfPermission(EscaneoActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
//TODO: Si no tiene los permisos, entonces se deberían solicitar via ActivityCompat.requestPermissions - https://developer.android.com/training/permissions/requesting.html?hl=es-419#perm-request
return;
}
try {
cameraSource.start(camara.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
cameraSource.stop();
}
});
detector.setProcessor(new Procesador(this));
}
public class Procesador implements Detector.Processor<Barcode> {
EscaneoActivity escaneoActivity;
public Procesador(EscaneoActivity e){
this.escaneoActivity = e;
}
@Override
public void release() {
}
@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
SparseArray<Barcode> objetosDetectados = detections.getDetectedItems();
if(objetosDetectados.size() > 0) {
Barcode barcode = objetosDetectados.valueAt(0);
EscaneoActivity.codigoActual = barcode;
Bundle b = new Bundle();
b.putLong(ConsultorProductosBD.X, Long.parseLong(barcode.displayValue));
escaneoActivity.realizarConsulta(b);
}
}
}
答案 0 :(得分:0)
如果你想在UI线程上运行某些东西,你可以使用它:
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
// your initloader command
}
});
但是如果您使用的是Loader
,则只要检测到更改,它就会自动查询。所以你做initLoader
一次,然后它就能帮到你。