0

我正在尝试使用他们的 ScannerViaIntent 源代码通过 zxing 从带有纯文本的条形码中提取数据。当我为单个 ImageButton 设置代码时,它工作得非常好,但是当我设置其他两个按钮时,我在从条形码扫描仪返回结果时收到此错误。

错误:

07-18 14:16:00.080: E/AndroidRuntime(9004): java.lang.RuntimeException: Unable to resume activity {com.fmi.inventory/com.fmi.inventory.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=49374, result=0, data=null} to activity {com.fmi.inventory/com.fmi.inventory.MainActivity}: java.lang.NullPointerException
07-18 14:16:00.080: E/AndroidRuntime(9004): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=49374, result=0, data=null} to activity {com.fmi.inventory/com.fmi.inventory.MainActivity}: java.lang.NullPointerException
07-18 14:16:00.080: E/AndroidRuntime(9004):     at com.fmi.inventory.MainActivity.onActivityResult(MainActivity.java:70)

代码:

package com.fmi.inventory;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

public class MainActivity extends Activity {

    ImageButton button;
    ImageButton button1;
    ImageButton button2;
    EditText editField;
    Activity activity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        activity = this;
        button = (ImageButton)findViewById(R.id.scanCubeID);
        button1 = (ImageButton)findViewById(R.id.scanEmployeeID);
        button2 = (ImageButton)findViewById(R.id.scanConfigID);
        button.setOnClickListener(listener);
        button1.setOnClickListener(listener);
        button2.setOnClickListener(listener);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();


    }
    private View.OnClickListener listener = new View.OnClickListener() {
        public void onClick(View v) {
            IntentIntegrator integrator = new IntentIntegrator(activity);
            switch (v.getId()){
                case (R.id.scanCubeID):
                    editField = (EditText)findViewById(R.id.editCubeID);
                    integrator.initiateScan();
                case (R.id.scanEmployeeID):
                    editField = (EditText)findViewById(R.id.editEmployeeID);
                    integrator.initiateScan();
                case (R.id.scanConfigID):
                    editField = (EditText)findViewById(R.id.editConfigID);
                    integrator.initiateScan();
            }


        }
    };

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
           IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
          if (scanResult != null) {
              String barcode = scanResult.getContents();
              this.editField.setText(barcode);
          }
            // else continue with any other code you need in the method

         }
}

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/editCubeID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/scanEmployeeID"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/scanCubeID"
        android:ems="10"
        android:hint="@string/edit_cubeid" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editEmployeeID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/scanConfigID"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/editCubeID"
        android:layout_below="@+id/editCubeID"
        android:ems="10"
        android:hint="@string/edit_employeeid" />

    <EditText
        android:id="@+id/editConfigID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/scanConfigID"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/editEmployeeID"
        android:layout_below="@+id/editEmployeeID"
        android:ems="10"
        android:hint="@string/edit_configid" />

    <ImageButton
        android:id="@+id/scanCubeID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:contentDescription="@string/desc"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_action_scan"/>

    <ImageButton
        android:id="@+id/scanEmployeeID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:contentDescription="@string/desc"
        android:layout_below="@+id/scanCubeID"
        android:src="@drawable/ic_action_scan"/>

    <ImageButton
        android:id="@+id/scanConfigID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:contentDescription="@string/desc"
        android:layout_below="@+id/scanEmployeeID"
        android:src="@drawable/ic_action_scan"/>

    <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editConfigID"
        android:text="@string/button_continue" />

</RelativeLayout>
4

1 回答 1

3

在 switch 中添加breakcase 为:

public void onClick(View v) {
            IntentIntegrator integrator = new IntentIntegrator(activity);
            switch (v.getId()){
                case (R.id.scanCubeID):
                    editField = (EditText)findViewById(R.id.editCubeID);
                    integrator.initiateScan();

                 break ;  // add here

                case (R.id.scanEmployeeID):
                    editField = (EditText)findViewById(R.id.editEmployeeID);
                    integrator.initiateScan();

                 break ;// add here

                case (R.id.scanConfigID):
                    editField = (EditText)findViewById(R.id.editConfigID);
                    integrator.initiateScan();

                 break ; // add here

            }


        }
    };
于 2012-07-18T18:21:49.293 回答