例外在听者附着到条形码应用多个按钮(Exception in listener attached t

2019-09-19 17:02发布

好了,所以我在家里条码扫描器开发一个用于当我们移动计算机和设备我公司使用。 我目前几乎通过一些试验和错误后设置斑马线条形码扫描器通过intent。

这里是我想要做的事。

接下来三个EditText领域我有三个ImageButtons ,单击时,实行BarcodeScanner ,扫描一次返回值和输入值入EditText领域。 我能做到这一点成功地利用一个“监听器”对应一个ImageButton 。 但是,试图使用多个后Buttons与一个监听器,它会调用条形码扫描器,但试图返回条码值时崩溃。

调试器显示挂起主线程,我要重新开始调试两倍之前RuntimeException出现在logcat的错误。

这里是从logcat的错误日志:

07-17 17:38:49.251:E / AndroidRuntime(30942):了java.lang.RuntimeException:无法恢复活动{com.fmi.inventory / com.fmi.inventory.MainActivity}:了java.lang.RuntimeException:失败递送结果ResultInfo {谁= NULL,请求= 49374,结果= -1,数据=意向{ACT = com.google.zxing.client.android.SCAN FLG = 0x80000(具有额外)}}到{活动com.fmi.inventory /com.fmi.inventory.MainActivity}:显示java.lang.NullPointerException

这里是我的MainActivity:

package com.fmi.inventory;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
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();

    }

    public void setCubeClick(){
        editField = (EditText)findViewById(R.id.editCubeID);
    }

    public void setEmployeeClick(){
        editField = (EditText)findViewById(R.id.editEmployeeID);
    }

    public void setConfigClick(){
        editField = (EditText)findViewById(R.id.editConfigID);
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        public void onClick(View v) {
            IntentIntegrator integrator = new IntentIntegrator(activity);
            integrator.initiateScan();

        }
    };

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        switch (requestCode) {
        case IntentIntegrator.REQUEST_CODE:
            if (resultCode == Activity.RESULT_OK) {

                IntentResult intentResult = IntentIntegrator
                        .parseActivityResult(requestCode, resultCode, intent);

                if (intentResult != null) {

                    String contents = intentResult.getContents();
                    String format = intentResult.getFormatName();
                    this.editField.setText(contents);
                    // this.elemQuery.setText(contents);
                    //this.resume = false;
                    Log.d("SEARCH_EAN", "OK, EAN: " + contents + ", FORMAT: "
                            + format);
                } else {
                    Log.e("SEARCH_EAN", "IntentResult je NULL!");
                }
            } else if (resultCode == Activity.RESULT_CANCELED) {
                Log.e("SEARCH_EAN", "CANCEL");
            }
        }
    }
}

这里是我的MainActivity布局的.xml

<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"
        android:onClick="onClick" />

    <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"
        android:onClick="onClick" />

    <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"
        android:onClick="onClick" />

    <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>

Answer 1:

当你的Activity都在onActivityResult scanning.You后的访问editField但它是NULL。

因为你从来没有初始化它..

要初始化它们setCubeClicksetEmployeeClicksetConfigClick

但我affraid他们从来没有called..Try来Initialize它们OnCreat



文章来源: Exception in listener attached to multiple buttons in barcode app