我有一个带有内置条形码扫描仪的android设备CipherLab RS31。我正在尝试以编程方式设置一些阅读器参数(支持的条形码,用户首选项等)。但是我总是得到一个例外
Attempt to invoke interface method 'int com.cipherlab.barcodebase.IBarcodeReaderServiceInterface.WriteOutputSettings(com.cipherlab.barcode.decoderparams.ReaderOutputConfiguration)' on a null object reference
这是我的代码:
public class BarCodeActivity extends Activity {
private ReaderManager readerManager;
private IntentFilter filter;
protected void onCreate(@Nullable Bundle savedInstanceState) {
readerManager = ReaderManager.InitInstance(this);
filter = new IntentFilter();
filter.addAction(GeneralString.Intent_PASS_TO_APP);
registerReceiver(broadcastReceiver, filter);
configureReader();
}
private void configureReader() {
Decoders decoders = new Decoders();
decoders.enableQRcode = Enable_State.TRUE;
//this is the line where the exception occurs
readerManager.Set_Decoders_Status(decoders);
}
}
调用Set_ReaderOutputConfiguration
类的以下方法Set_Symbology
,Set_UserPreferences
,ReaderManager
时,会发生相同的异常。
答案 0 :(得分:0)
在阅读器管理器上执行任何工作之前,您需要等待GeneralString.Intent_READERSERVICE_CONNECTED意图触发:
filter = new IntentFilter();
filter.addAction(GeneralString.Intent_PASS_TO_APP);
registerReceiver(broadcastReceiver, filter);
然后在您的BroadcastReceiver实现中:
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(GeneralString.Intent_READERSERVICE_CONNECTED)){
// do configuration here
}
}