使用zxing扫描后将数据保存到数据库中时出错

时间:2017-12-29 11:13:08

标签: android android-sqlite zxing

应该做什么:使用已整合的ZXING扫描条形码并将条形码保存到应用的SQLite database

首先,我尝试扫描条形码并使用Toast-Message在屏幕上显示它到目前为止它工作得很好。

之后,我添加了将barcode插入数据库的功能。从那以后我收到了以下错误:

12-29 11:58:00.363 28012-28012/com.factory.future.ap_manager E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.factory.future.ap_manager, PID: 28012
                                                                           java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=49374, result=-1, data=Intent { act=com.google.zxing.client.android.SCAN flg=0x80000 launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } (has extras) }} to activity {com.factory.future.ap_manager/com.factory.future.ap_manager.MainActivity}: java.lang.IllegalArgumentException: Unknown URL content://com.factory.future.ap_manager/aps
                                                                               at android.app.ActivityThread.deliverResults(ActivityThread.java:4520)
                                                                               at android.app.ActivityThread.handleSendResult(ActivityThread.java:4563)
                                                                               at android.app.ActivityThread.-wrap22(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1698)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
                                                                            Caused by: java.lang.IllegalArgumentException: Unknown URL content://com.factory.future.ap_manager/aps
                                                                               at android.content.ContentResolver.insert(ContentResolver.java:1276)
                                                                               at com.factory.future.ap_manager.MainActivity.addAP(MainActivity.java:116)
                                                                               at com.factory.future.ap_manager.MainActivity.onActivityResult(MainActivity.java:95)
                                                                               at android.app.Activity.dispatchActivityResult(Activity.java:7282)
                                                                               at android.app.ActivityThread.deliverResults(ActivityThread.java:4516)
                                                                               at android.app.ActivityThread.handleSendResult(ActivityThread.java:4563) 
                                                                               at android.app.ActivityThread.-wrap22(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1698) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:154) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6776) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 

以下是处理扫描结果的代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if(result != null) {
        if(result.getContents() == null) {
            Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
        } else {
            addAP(result.getContents());
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

private void addAP(String barcodeNumber) {

    int codeLength = barcodeNumber.length();
    barcodeNumber = barcodeNumber.substring(4,codeLength - 1);
    int APID = Integer.parseInt(barcodeNumber);
    //Toast.makeText(this, "Scanned: " + APID, Toast.LENGTH_LONG).show();

    String title = "TestAP-" + barcodeNumber;

    ContentValues values = new ContentValues();
    values.put(APEntry.AP_ID, 13222);
    values.put(APEntry.AP_TITLE, title);

    Toast.makeText(this, APEntry.CONTENT_URI.toString(), Toast.LENGTH_LONG).show();
    getContentResolver().insert(APEntry.CONTENT_URI, values);

}

我还尝试将Intent跳到ZXING并直接调用addAP方法中的onClick函数。在那里得到以下错误:

java.lang.IllegalArgumentException: Unknown URL content://com.factory.future.ap_manager/aps

这是所有文件的链接: Link

1 个答案:

答案 0 :(得分:0)

问题解决了! CONTENT_URI本身也是正确的,但在AndroidManifest.xml我写了android:authorities="com.future.factory.ap_manager"而不是android:authorities="com.factory.future.ap_manager"。 (在查看代码时错过了数百次)