2016-07-18 92 views
0

我剛剛開始涉足Android開發中稍微複雜的事情。我試圖在我的應用程序中設置一個頁面,使其具有一個打開「簡單相機」的按鈕,帶有複選框,然後返回圖像的縮略圖並使用它來處理條形碼。點擊頁面上的複選框後,我的應用程序似乎崩潰了,我一直在關注如何讓這個API工作的google codelabs教程。也許這是由於圖像是縮略圖的事實?如果你們可以幫忙,這將是非常棒的。Android Vision API條形碼圖像返回

public class AddItemActivity extends AppCompatActivity { 

    static final int REQUEST_IMAGE_CAPTURE = 1; 
    public TextView txtView; 
    BarcodeDetector detector; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getSupportActionBar().setTitle("Add Item"); 
     setContentView(R.layout.activity_add_item); 
    } 

    public void ItemInfo(View view){ 
     Intent intent = new Intent(this, ItemInfoActivity.class); 
     AddItemActivity.this.startActivity(intent); 
    } 

    public void QRLookup(View view){ 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); 
    } 

    public void onActivityResult(int requestCode, int resultCode, Intent data){ 
     if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { 
      txtView = (TextView) this.findViewById(R.id.ResultQR); 
      detector = 
        new BarcodeDetector.Builder(getApplicationContext()) 
          .setBarcodeFormats(Barcode.DATA_MATRIX | Barcode.QR_CODE) 
          .build(); 
      if(!detector.isOperational()){ 
       txtView.setText("Could not set up the detector!"); 
       return;} 
      Bundle extras = data.getExtras(); 
      Bitmap imageBitmap = (Bitmap) extras.get("data"); 
      Frame frame = new Frame.Builder().setBitmap(imageBitmap).build(); 
      SparseArray<Barcode> barcodes = detector.detect(frame); 
      Barcode thisCode = barcodes.valueAt(0); 
      txtView.setText(thisCode.rawValue); 
     } 
    } 
} 

這是我的活動。這是我的xml。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:background="#383838" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingLeft="40dp" 
    android:paddingRight="40dp" 
    android:paddingTop="20dp" 
    android:paddingBottom="100dp" 
    tools:context=".AddItemActivity"> 
//Buttons 
    <Button 
     android:layout_marginTop="30dp" 
     android:layout_width="125dp" 
     android:layout_gravity="center" 
     android:layout_height="30dp" 
     android:text="Add Item" 
     android:background="#31be31" 
     android:onClick="ItemInfo" /> 
    <Button 
     android:layout_marginTop="30dp" 
     android:layout_width="125dp" 
     android:layout_gravity="center" 
     android:layout_height="60dp" 
     android:background="#31be31" 
     android:text="QR LOOKUP" 
     android:onClick="QRLookup"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/ResultQR" /> 

</LinearLayout> 

最後是我的崩潰日誌,或者我的Android監視器,因爲它在Android Studio中調用。

07-18 09:33:12.890 16396-16396/com.arcx.cranium I/art: Late-enabling -Xcheck:jni 
07-18 09:33:12.939 16396-16396/com.arcx.cranium W/System: ClassLoader referenced unknown path: /data/app/com.arcx.cranium-2/lib/arm64 
07-18 09:33:22.187 16396-16396/com.arcx.cranium W/System: ClassLoader referenced unknown path: /data/app/com.arcx.cranium-2/lib/arm64 
07-18 09:33:22.389 16396-16396/com.arcx.cranium W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
07-18 09:33:22.589 16396-16856/com.arcx.cranium D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 
07-18 09:33:22.684 16396-16856/com.arcx.cranium I/Adreno: QUALCOMM build     : fcf4852, I1af360237c 
                  Build Date      : 09/23/15 
                  OpenGL ES Shader Compiler Version: XE031.05.13.02 
                  Local Branch      : mybranch14465806 
                  Remote Branch     : quic/LA.BF64.1.2.9_v2 
                  Remote Branch     : NONE 
                  Reconstruct Branch    : NOTHING 
07-18 09:33:22.738 16396-16856/com.arcx.cranium I/OpenGLRenderer: Initialized EGL, version 1.4 
07-18 09:34:45.808 16396-16396/com.arcx.cranium D/ChimeraCfgMgr: Reading stored module config 
07-18 09:34:45.884 16396-16396/com.arcx.cranium W/System: ClassLoader referenced unknown path: /data/user/0/com.google.android.gms/app_chimera/m/00000004/n/arm64-v8a 
07-18 09:34:45.898 16396-16396/com.arcx.cranium D/ChimeraFileApk: Primary ABI of requesting process is arm64-v8a 
07-18 09:34:45.902 16396-16396/com.arcx.cranium D/ChimeraFileApk: Classloading successful. Optimized code found. 
07-18 09:34:45.963 16396-16396/com.arcx.cranium D/AndroidRuntime: Shutting down VM 
07-18 09:34:45.963 16396-16396/com.arcx.cranium E/AndroidRuntime: FATAL EXCEPTION: main 
                    Process: com.arcx.cranium, PID: 16396 
                    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.arcx.cranium/com.arcx.cranium.AddItemActivity}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 
                     at android.app.ActivityThread.deliverResults(ActivityThread.java:3699) 
                     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
                     at android.app.ActivityThread.-wrap16(ActivityThread.java) 
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                     at android.os.Looper.loop(Looper.java:148) 
                     at android.app.ActivityThread.main(ActivityThread.java:5417) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                    Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 
                     at android.util.SparseArray.valueAt(SparseArray.java:291) 
                     at com.arcx.cranium.AddItemActivity.onActivityResult(AddItemActivity.java:55) 
                     at android.app.Activity.dispatchActivityResult(Activity.java:6428) 
                     at android.app.ActivityThread.deliverResults(ActivityThread.java:3695) 
                     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)  
                     at android.app.ActivityThread.-wrap16(ActivityThread.java)  
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)  
                     at android.os.Handler.dispatchMessage(Handler.java:102)  
                     at android.os.Looper.loop(Looper.java:148)  
                     at android.app.ActivityThread.main(ActivityThread.java:5417)  
                     at java.lang.reflect.Method.invoke(Native Method)  
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
07-18 09:34:46.967 16396-17395/com.arcx.cranium I/Vision: Connection error: null 
07-18 09:34:47.969 16396-17395/com.arcx.cranium I/Vision: Connection error: null 
07-18 09:34:50.965 16396-16396/com.arcx.cranium I/Process: Sending signal. PID: 16396 SIG: 9 
07-18 09:34:51.116 17414-17414/com.arcx.cranium W/System: ClassLoader referenced unknown path: /data/app/com.arcx.cranium-2/lib/arm64 
07-18 09:34:51.343 17414-17414/com.arcx.cranium W/System: ClassLoader referenced unknown path: /data/app/com.arcx.cranium-2/lib/arm64 
07-18 09:34:51.554 17414-17414/com.arcx.cranium W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
07-18 09:34:51.914 17414-17444/com.arcx.cranium D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 
07-18 09:34:51.985 17414-17444/com.arcx.cranium I/Adreno: QUALCOMM build     : fcf4852, I1af360237c 
                  Build Date      : 09/23/15 
                  OpenGL ES Shader Compiler Version: XE031.05.13.02 
                  Local Branch      : mybranch14465806 
                  Remote Branch     : quic/LA.BF64.1.2.9_v2 
                  Remote Branch     : NONE 
                  Reconstruct Branch    : NOTHING 
07-18 09:34:51.995 17414-17444/com.arcx.cranium I/OpenGLRenderer: Initialized EGL, version 1.4 

謝謝你們了很多,我不完全知道如何解釋某些崩潰日誌,我不是100%確定爲如何條形碼API的工作。我試圖用最簡單的方式製作這個應用程序,然後從那裏向上移動。 :)

回答

1

As your java.lang.ArrayIndexOutOfBoundsException:length = 0; index = 0異常說,你試圖訪問一個空數組的第一項。

在這裏:

SparseArray<Barcode> barcodes = detector.detect(frame); 
    Barcode thisCode = barcodes.valueAt(0); 

您獲取條形碼後,請確保您的SparseArray是不是訪問它的元素之前空。

SparseArray<Barcode> barcodes = detector.detect(frame); 
    if (barcodes.size() > 0) { 
      Barcode thisCode = barcodes.valueAt(0); 
      txtView.setText(thisCode.rawValue); 
    }