例外监听器附着在酒吧code应用多个按钮 [英] Exception in listener attached to multiple buttons in barcode app

查看:157
本文介绍了例外监听器附着在酒吧code应用多个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我开发一个室内酒吧code扫描仪,当我们移动计算机和设备我公司使用。我目前几乎通过一些试验和错误后设置斑马线酒吧code扫描仪通过意向。

下面是我想要做的。

接下来三个的EditText 字段我有三个 ImageButtons 该按钮时,执行酒吧codeScanner ,扫描一次返回值和输入值到的EditText 字段。我能够做到这一点成功使用一个监听器​​对应一个的ImageButton 。但是,试图使用多个按钮一个监听器后,它试图返回酒吧code值时调用bar code扫描仪,但崩溃。

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

下面是从LogCat中的错误日志:


  

17 07-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:

 包com.fmi.inventory;进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.Menu;
进口android.view.View;
进口android.widget.EditText;
进口android.widget.ImageButton;公共类MainActivity延伸活动{    的ImageButton按钮;
    的ImageButton按钮1;
    的ImageButton按钮2;
    EDITFIELD的EditText;
    活动活动;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        活性=这一点;
        按钮=(的ImageButton)findViewById(R.id.scanCubeID);
        BUTTON1 =(的ImageButton)findViewById(R.id.scanEmployeeID);
        按钮2 =(的ImageButton)findViewById(R.id.scanConfigID);
        button.setOnClickListener(监听);
        button1.setOnClickListener(监听);
        button2.setOnClickListener(监听);    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }    @覆盖
    公共无效onResume(){
        super.onResume();    }    公共无效setCubeClick(){
        EDITFIELD =(EditText上)findViewById(R.id.editCubeID);
    }    公共无效setEmployeeClick(){
        EDITFIELD =(EditText上)findViewById(R.id.editEmployeeID);
    }    公共无效setConfigClick(){
        EDITFIELD =(EditText上)findViewById(R.id.editConfigID);
    }    私人View.OnClickListener监听器=新View.OnClickListener(){
        公共无效的onClick(视图v){
            IntentIntegrator积分=新IntentIntegrator(活动);
            integrator.initiateScan();        }
    };    公共无效的onActivityResult(INT申请code,INT结果code,意图意图){
        开关(要求code){
        案例IntentIntegrator.REQUEST_ code:
            如果(结果code == Activity.RESULT_OK){                IntentResult intentResult = IntentIntegrator
                        .parseActivityResult(要求code,结果code,意向);                如果(intentResult!= NULL){                    字符串内容= intentResult.getContents();
                    字符串格式= intentResult.getFormatName();
                    this.editField.setText(内容);
                    // this.elemQuery.setText(内容);
                    //this.resume = FALSE;
                    Log.d(SEARCH_EAN,OK,EAN:+内容+,格式为:
                            +格式);
                }其他{
                    Log.e(SEARCH_EAN,IntentResult济空!);
                }
            }否则如果(结果code == Activity.RESULT_CANCELED){
                Log.e(SEARCH_EAN,取消);
            }
        }
    }
}

下面是我的MainActivity布局的.xml

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>    <的EditText
        机器人:ID =@ + ID / editCubeID
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ + ID / scanEmployeeID
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentTop =真
        机器人:layout_toLeftOf =@ + ID / scanCubeID
        机器人:EMS =10
        机器人:提示=@字符串/ edit_cubeid>        < requestFocus的/>
    < /&的EditText GT;    <的EditText
        机器人:ID =@ + ID / editEmployeeID
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ + ID / scanConfigID
        机器人:layout_alignParentLeft =真
        机器人:layout_alignRight =@ + ID / editCubeID
        机器人:layout_below =@ + ID / editCubeID
        机器人:EMS =10
        机器人:提示=@字符串/ edit_employeeid/>    <的EditText
        机器人:ID =@ + ID / editConfigID
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBottom =@ + ID / scanConfigID
        机器人:layout_alignParentLeft =真
        机器人:layout_alignRight =@ + ID / editEmployeeID
        机器人:layout_below =@ + ID / editEmployeeID
        机器人:EMS =10
        机器人:提示=@字符串/ edit_configid/>    <的ImageButton
        机器人:ID =@ + ID / scanCubeID
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:contentDescription =@字符串/ DESC
        机器人:layout_alignParentTop =真
        机器人:SRC =@绘制/ ic_action_scan
        安卓的onClick =的onClick/>    <的ImageButton
        机器人:ID =@ + ID / scanEmployeeID
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:contentDescription =@字符串/ DESC
        机器人:layout_below =@ + ID / scanCubeID
        机器人:SRC =@绘制/ ic_action_scan
        安卓的onClick =的onClick/>    <的ImageButton
        机器人:ID =@ + ID / scanConfigID
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:contentDescription =@字符串/ DESC
        机器人:layout_below =@ + ID / scanEmployeeID
        机器人:SRC =@绘制/ ic_action_scan
        安卓的onClick =的onClick/>    <按钮
        机器人:ID =@ + ID /提交
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID / editConfigID
        机器人:文字=@字符串/ button_continue/>< / RelativeLayout的>


解决方案

在你的活动进来的onActivityResult 之后scanning.You正在访问 EDITFIELD 但它的 NULL

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

您正在初始化它们 setCubeClick setEmployeeClick setConfigClick

但我affraid他们从来没有向called..Try 初始化他们 OnCreat

Okay, So I am developing an in house barcode scanner for my company to use when we move computers and equipment. I am currently almost through setting up the Zxing Barcode Scanner Via Intent after some trial and error.

Here is what I'm trying to do.

Next to three EditText fields I have Three ImageButtons that when clicked, implement the BarcodeScanner, once scanned returns the value and inputs the value into the EditText field. I was able to do it successfully using one "listener" corresponding to one ImageButton. But after trying to use multiple Buttons with one listener, it calls the barcode scanner but crashes when trying to return the barcode value.

The Debugger shows the main Thread Suspended and I have to resume the debugger twice before the RuntimeException error appears in Logcat.

here's the error log from LogCat:

07-17 17:38:49.251: E/AndroidRuntime(30942): 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=-1, data=Intent { act=com.google.zxing.client.android.SCAN flg=0x80000 (has extras) }} to activity {com.fmi.inventory/com.fmi.inventory.MainActivity}: java.lang.NullPointerException

Here is my 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");
            }
        }
    }
}

Here is my layout .xml for MainActivity

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

解决方案

When your Activity come in onActivityResult after scanning.You are accessing editField but it is NULL.

as you never Initialized it..

You are Initializing them in setCubeClick, setEmployeeClick or setConfigClick,

But I am affraid they never called..Try to Initialize them in OnCreat

这篇关于例外监听器附着在酒吧code应用多个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆