gpt4 book ai didi

android - 附加到条形码应用程序中多个按钮的监听器异常

转载 作者:行者123 更新时间:2023-11-29 02:03:05 25 4
gpt4 key购买 nike

好的,所以我正在为我的公司开发一个内部条形码扫描仪,以便在我们移动计算机和设备时使用。经过一些试验和错误后,我目前几乎通过 Intent 设置了 Zxing 条码扫描仪。

这是我正在尝试做的事情。

在三个 EditText 字段旁边,我有三个 ImageButtons,单击它们时,实现 BarcodeScanner,扫描后返回值并输入值进入 EditText 字段。我能够使用一个对应于一个 ImageButton 的“监听器”成功地做到这一点。但是在尝试对一个监听器使用多个 Button 后,它会调用条形码扫描仪,但在尝试返回条形码值时会崩溃。

调试器显示主线程已暂停,我必须在 Logcat 中出现 RuntimeException 错误之前恢复调试器两次。

这是来自 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

这是我的 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>

最佳答案

当你的Activity扫描后进入onActivityResult。你正在访问editField,但它是NULL

因为你从未初始化它..

您正在 setCubeClicksetEmployeeClicksetConfigClick 中初始化它们,

但我担心他们从未调用过..尝试在OnCreatInitialize他们

关于android - 附加到条形码应用程序中多个按钮的监听器异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11544674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com