ZXing интегрирован в приложение для Android (включая PhoneGap)

#android #cordova

#Android #кордова

Вопрос:

Я хочу использовать ZXing для декодирования штрих-кода в моем приложении для Android. Я использую Phonegap для создания приложения. Теперь я хочу интегрировать ZXing для сканирования штрих-кода и обработки результата в моем приложении. Я использую Android 2.1 — update1 SDK.

Во-первых, я интегрировал следующие классы в свое приложение:

  • IntentIntegrator.java
  • IntentResult.java

Затем я создал действие под названием «Scanactivity» со следующим содержимым:

 import android.app.Activity;
import android.content.Intent;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResu<
public class ScanActivity extends Activity {
  public void scanCode() {
    IntentIntegrator.initiateScan(ScanActivity.this);
  }
  public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    if (scanResult != null) {
      // handle scan result
    }
    // else continue with any other code you need in the method
  }
}
  

В представлении я разместил кнопку, которая вызывает метод scanCode () нажатием.

 $("#button_scan").click(function(e) {
  ScanActivity.scanCode();
});
  

Я установил приложение «Сканер штрих-кода (ZXing)» на целевом устройстве, чтобы использовать функцию intent. Если я нажимаю кнопку сканирования в своем приложении, приложение выходит из строя и закрывается. В чем моя ошибка?

Вот logcat:

 04-13 10:20:46.636: WARN/dalvikvm(2525): JNI WARNING: DeleteLocalRef(0x44caf3c0) failed to find entry (valid=1)
04-13 10:20:46.636: WARN/dalvikvm(2525): JNI WARNING: DeleteLocalRef(0x44caf3c0) failed to find entry (valid=1)
04-13 10:20:46.636: ERROR/dalvikvm(2525): Class lookup Landroid/webkit/WebHistoryItem; attemped while exception Ljava/lang/NullPointerException; pending
04-13 10:20:46.636: INFO/dalvikvm(2525): DALVIK THREADS:
04-13 10:20:46.636: INFO/dalvikvm(2525): "main" prio=5 tid=3 WAIT
04-13 10:20:46.636: INFO/dalvikvm(2525):   | group="main" sCount=0 dsCount=0 s=N obj=0x4001e250 self=0xbe30
04-13 10:20:46.636: INFO/dalvikvm(2525):   | sysTid=2525 nice=0 sched=0/0 cgrp=unknown handle=-1344001356
04-13 10:20:46.636: INFO/dalvikvm(2525):   at java.lang.Object.wait(Native Method)
04-13 10:20:46.636: INFO/dalvikvm(2525):   - waiting on <0x2488a8> (a android.os.MessageQueue)
04-13 10:20:46.636: INFO/dalvikvm(2525):   at java.lang.Object.wait(Object.java:288)
04-13 10:20:46.636: INFO/dalvikvm(2525):   at android.os.MessageQueue.next(MessageQueue.java:148)
04-13 10:20:46.636: INFO/dalvikvm(2525):   at android.os.Looper.loop(Looper.java:110)
04-13 10:20:46.636: INFO/dalvikvm(2525):   at android.app.ActivityThread.main(ActivityThread.java:4363)
04-13 10:20:46.636: INFO/dalvikvm(2525):   at java.lang.reflect.Method.invokeNative(Native Method)
04-13 10:20:46.636: INFO/dalvikvm(2525):   at java.lang.reflect.Method.invoke(Method.java:521)
04-13 10:20:46.636: INFO/dalvikvm(2525):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-13 10:20:46.636: INFO/dalvikvm(2525):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-13 10:20:46.636: INFO/dalvikvm(2525):   at dalvik.system.NativeStart.main(Native Method)
04-13 10:20:46.646: INFO/dalvikvm(2525): "http3" prio=5 tid=31 WAIT
04-13 10:20:46.646: INFO/dalvikvm(2525):   | group="main" sCount=0 dsCount=0 s=N obj=0x44c970a8 self=0x296f40
04-13 10:20:46.646: INFO/dalvikvm(2525):   | sysTid=2544 nice=1 sched=0/0 cgrp=unknown handle=2715736
04-13 10:20:46.646: INFO/dalvikvm(2525):   at java.lang.Object.wait(Native Method)
04-13 10:20:46.646: INFO/dalvikvm(2525):   - waiting on <0x28cff0> (a android.net.http.RequestQueue)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at java.lang.Object.wait(Object.java:288)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at android.net.http.ConnectionThread.run(ConnectionThread.java:98)
04-13 10:20:46.646: INFO/dalvikvm(2525): "http2" prio=5 tid=29 WAIT
04-13 10:20:46.646: INFO/dalvikvm(2525):   | group="main" sCount=0 dsCount=0 s=N obj=0x44c96e58 self=0x294f00
04-13 10:20:46.646: INFO/dalvikvm(2525):   | sysTid=2543 nice=1 sched=0/0 cgrp=unknown handle=2707480
04-13 10:20:46.646: INFO/dalvikvm(2525):   at java.lang.Object.wait(Native Method)
04-13 10:20:46.646: INFO/dalvikvm(2525):   - waiting on <0x28cff0> (a android.net.http.RequestQueue)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at java.lang.Object.wait(Object.java:288)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at android.net.http.ConnectionThread.run(ConnectionThread.java:98)
04-13 10:20:46.646: INFO/dalvikvm(2525): "http1" prio=5 tid=27 WAIT
04-13 10:20:46.646: INFO/dalvikvm(2525):   | group="main" sCount=0 dsCount=0 s=N obj=0x44c96ba0 self=0x294cf8
04-13 10:20:46.646: INFO/dalvikvm(2525):   | sysTid=2542 nice=1 sched=0/0 cgrp=unknown handle=2706960
04-13 10:20:46.646: INFO/dalvikvm(2525):   at java.lang.Object.wait(Native Method)
04-13 10:20:46.646: INFO/dalvikvm(2525):   - waiting on <0x28cff0> (a android.net.http.RequestQueue)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at java.lang.Object.wait(Object.java:288)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at android.net.http.ConnectionThread.run(ConnectionThread.java:98)
04-13 10:20:46.646: INFO/dalvikvm(2525): "http0" prio=5 tid=25 WAIT
04-13 10:20:46.646: INFO/dalvikvm(2525):   | group="main" sCount=0 dsCount=0 s=N obj=0x44c96990 self=0x294be0
04-13 10:20:46.646: INFO/dalvikvm(2525):   | sysTid=2541 nice=1 sched=0/0 cgrp=unknown handle=2667208
04-13 10:20:46.646: INFO/dalvikvm(2525):   at java.lang.Object.wait(Native Method)
04-13 10:20:46.646: INFO/dalvikvm(2525):   - waiting on <0x28cff0> (a android.net.http.RequestQueue)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at java.lang.Object.wait(Object.java:288)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at android.net.http.ConnectionThread.run(ConnectionThread.java:98)
04-13 10:20:46.646: INFO/dalvikvm(2525): "Thread-12" prio=5 tid=23 TIMED_WAIT
04-13 10:20:46.646: INFO/dalvikvm(2525):   | group="main" sCount=0 dsCount=0 s=N obj=0x44c7b460 self=0x261498
04-13 10:20:46.646: INFO/dalvikvm(2525):   | sysTid=2540 nice=0 sched=0/0 cgrp=unknown handle=2375456
04-13 10:20:46.646: INFO/dalvikvm(2525):   at java.lang.Object.wait(Native Method)
04-13 10:20:46.646: INFO/dalvikvm(2525):   - waiting on <0x2444b0> (a com.phonegap.DroidGap$1$1)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at java.lang.Object.wait(Object.java:326)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at com.phonegap.DroidGap$1$1.run(DroidGap.java:387)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at java.lang.Thread.run(Thread.java:1096)
04-13 10:20:46.646: INFO/dalvikvm(2525): "Thread-11" prio=5 tid=21 NATIVE
04-13 10:20:46.646: INFO/dalvikvm(2525):   | group="main" sCount=0 dsCount=0 s=N obj=0x44c7af48 self=0x262998
04-13 10:20:46.646: INFO/dalvikvm(2525):   | sysTid=2539 nice=0 sched=0/0 cgrp=unknown handle=2333912
04-13 10:20:46.646: INFO/dalvikvm(2525):   at org.apache.harmony.luni.platform.OSNetworkSystem.acceptSocketImpl(Native Method)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at org.apache.harmony.luni.platform.OSNetworkSystem.accept(OSNetworkSystem.java:69)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at org.apache.harmony.luni.net.PlainSocketImpl.accept(PlainSocketImpl.java:126)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at java.net.ServerSocket.implAccept(ServerSocket.java:285)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at java.net.ServerSocket.accept(ServerSocket.java:164)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at com.phonegap.CallbackServer.run(CallbackServer.java:185)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at java.lang.Thread.run(Thread.java:1096)
04-13 10:20:46.646: INFO/dalvikvm(2525): "CookieSyncManager" prio=5 tid=19 TIMED_WAIT
04-13 10:20:46.646: INFO/dalvikvm(2525):   | group="main" sCount=0 dsCount=0 s=N obj=0x44c68588 self=0x127408
04-13 10:20:46.646: INFO/dalvikvm(2525):   | sysTid=2538 nice=10 sched=0/0 cgrp=unknown handle=1220392
04-13 10:20:46.646: INFO/dalvikvm(2525):   at java.lang.Object.wait(Native Method)
04-13 10:20:46.646: INFO/dalvikvm(2525):   - waiting on <0x23b538> (a android.os.MessageQueue)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at java.lang.Object.wait(Object.java:326)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at android.os.MessageQueue.next(MessageQueue.java:144)
04-13 10:20:46.646: INFO/dalvikvm(2525):   at android.os.Looper.loop(Looper.java:110)
04-13 10:20:46.656: INFO/dalvikvm(2525):   at android.webkit.WebSyncManager.run(WebSyncManager.java:90)
04-13 10:20:46.656: INFO/dalvikvm(2525):   at java.lang.Thread.run(Thread.java:1096)
04-13 10:20:46.656: INFO/dalvikvm(2525): "WebViewCoreThread" prio=5 tid=17 RUNNABLE
04-13 10:20:46.656: INFO/dalvikvm(2525):   | group="main" sCount=0 dsCount=0 s=N obj=0x44c63210 self=0x136478
04-13 10:20:46.656: INFO/dalvikvm(2525):   | sysTid=2535 nice=0 sched=0/0 cgrp=unknown handle=1339552
04-13 10:20:46.656: INFO/dalvikvm(2525):   at android.webkit.WebViewCore.nativeTouchUp(Native Method)
04-13 10:20:46.656: INFO/dalvikvm(2525):   at android.webkit.WebViewCore.access$3400(WebViewCore.java:48)
04-13 10:20:46.656: INFO/dalvikvm(2525):   at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1094)
04-13 10:20:46.656: INFO/dalvikvm(2525):   at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 10:20:46.656: INFO/dalvikvm(2525):   at android.os.Looper.loop(Looper.java:123)
04-13 10:20:46.656: INFO/dalvikvm(2525):   at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:611)
04-13 10:20:46.656: INFO/dalvikvm(2525):   at java.lang.Thread.run(Thread.java:1096)
04-13 10:20:46.656: INFO/dalvikvm(2525): "Binder Thread #3" prio=5 tid=15 NATIVE
04-13 10:20:46.656: INFO/dalvikvm(2525):   | group="main" sCount=0 dsCount=0 s=N obj=0x44c530e8 self=0x130c70
04-13 10:20:46.656: INFO/dalvikvm(2525):   | sysTid=2534 nice=0 sched=0/0 cgrp=unknown handle=1354864
04-13 10:20:46.656: INFO/dalvikvm(2525):   at dalvik.system.NativeStart.run(Native Method)
04-13 10:20:46.656: INFO/dalvikvm(2525): "Binder Thread #2" prio=5 tid=13 NATIVE
04-13 10:20:46.656: INFO/dalvikvm(2525):   | group="main" sCount=0 dsCount=0 s=N obj=0x44c53028 self=0x1269e0
04-13 10:20:46.656: INFO/dalvikvm(2525):   | sysTid=2533 nice=0 sched=0/0 cgrp=unknown handle=1383744
04-13 10:20:46.656: INFO/dalvikvm(2525):   at dalvik.system.NativeStart.run(Native Method)
04-13 10:20:46.656: INFO/dalvikvm(2525): "Binder Thread #1" prio=5 tid=11 NATIVE
04-13 10:20:46.656: INFO/dalvikvm(2525):   | group="main" sCount=0 dsCount=0 s=N obj=0x44c4dc50 self=0x132840
04-13 10:20:46.656: INFO/dalvikvm(2525):   | sysTid=2532 nice=0 sched=0/0 cgrp=unknown handle=1354576
04-13 10:20:46.656: INFO/dalvikvm(2525):   at dalvik.system.NativeStart.run(Native Method)
04-13 10:20:46.656: INFO/dalvikvm(2525): "JDWP" daemon prio=5 tid=9 VMWAIT
04-13 10:20:46.656: INFO/dalvikvm(2525):   | group="system" sCount=0 dsCount=0 s=N obj=0x44c4c2a0 self=0x14e0f0
04-13 10:20:46.656: INFO/dalvikvm(2525):   | sysTid=2529 nice=0 sched=0/0 cgrp=unknown handle=1135864
04-13 10:20:46.656: INFO/dalvikvm(2525):   at dalvik.system.NativeStart.run(Native Method)
04-13 10:20:46.656: INFO/dalvikvm(2525): "Signal Catcher" daemon prio=5 tid=7 VMWAIT
04-13 10:20:46.656: INFO/dalvikvm(2525):   | group="system" sCount=0 dsCount=0 s=N obj=0x44c4c1e8 self=0x1127b0
04-13 10:20:46.656: INFO/dalvikvm(2525):   | sysTid=2528 nice=0 sched=0/0 cgrp=unknown handle=1338232
04-13 10:20:46.656: INFO/dalvikvm(2525):   at dalvik.system.NativeStart.run(Native Method)
04-13 10:20:46.656: INFO/dalvikvm(2525): "HeapWorker" daemon prio=5 tid=5 VMWAIT
04-13 10:20:46.656: INFO/dalvikvm(2525):   | group="system" sCount=0 dsCount=0 s=N obj=0x43562ed8 self=0x12b268
04-13 10:20:46.656: INFO/dalvikvm(2525):   | sysTid=2526 nice=0 sched=0/0 cgrp=unknown handle=1355192
04-13 10:20:46.656: INFO/dalvikvm(2525):   at dalvik.system.NativeStart.run(Native Method)
04-13 10:20:46.656: ERROR/dalvikvm(2525): VM aborting
  

Это стек:

 ScanActivity.scanCode() line: 12    
    IntentIntegrator.initiateScan(ScanActivity.this);

Activity.class:
    ScanActivity(Activity).startActivityForResult(Intent, int) line: 2749   

Reference.class:
    WeakReference(Reference).get() line: 147    
  

Затем приложение завершает работу.

Комментарии:

1. Какое исключение приводит к сбою приложения? Вы заглядывали в Logcat?

2. Это не LogCat. Или, по крайней мере, это не трассировка стека Java из вашего исключения.

3. я добавил стек в сообщение.

Ответ №1:

Ознакомьтесь с руководством по штрих-коду PhoneGap здесь. Шаг за шагом вы перейдете к созданию образца приложения с zxing с помощью плагина PhoneGap от purplecabbage.