Android 将OnClickListener与多个ImageButtons一起使用?
我试图通过zxing使用ScannerViaIntent源代码从带有纯文本的条形码中提取数据。当我为一个ImageButton设置代码时,它工作得非常好,但是当我设置其他两个按钮时,当从条形码扫描仪返回结果时,我收到这个错误 错误:Android 将OnClickListener与多个ImageButtons一起使用?,android,android-intent,zxing,Android,Android Intent,Zxing,我试图通过zxing使用ScannerViaIntent源代码从带有纯文本的条形码中提取数据。当我为一个ImageButton设置代码时,它工作得非常好,但是当我设置其他两个按钮时,当从条形码扫描仪返回结果时,我收到这个错误 错误: 07-18 14:16:00.080: E/AndroidRuntime(9004): java.lang.RuntimeException: Unable to resume activity {com.fmi.inventory/com.fmi.inventor
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)
代码:
布局:
<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>
为开关组件中的案例添加
中断
:
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
}
}
};
为开关中的情况添加
break
,如下所示:
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
}
}
};
您是否对项目进行了多次清理以查看问题是否得到解决?MainActivity.java文件中第70行的代码是什么。在那一行,您得到的是NullPointerException。可能的重复项您是否已清理了项目几次,以查看问题是否已解决?MainActivity.java文件中第70行的代码是什么。在那一行,你得到的是NullPointerException。可能是的重复,但我不确定这与onActivityResult(…)中的NPE有什么关系。虽然break很重要,但我认为是其他原因破坏了他的代码。他得到的“this.edittext”的空指针我在他之前的问题中提到了他的NPE。但不知道他为什么看不到……谢谢@imran khan;我不知道为什么我忘了休息@Alice@M Mohsin Naeem在另一篇帖子上修复了我的问题。我不小心复制了它们。我没有在
onCreate
中初始化editField
,只是在onacityryresult
中初始化了NULL
值。除了我不确定这与onActivityResult(…)
中的NPE有什么关系外,中断很重要,我认为是别的什么东西破坏了他的代码。他得到了一个空指针“this.edittext”,我在他之前的问题中提到了他的NPE。但不知道他为什么看不到……谢谢@imran khan;我不知道为什么我忘了休息@Alice@M Mohsin Naeem在另一篇帖子上修复了我的问题。我不小心复制了它们。我没有在onCreate
中初始化editField
,只是在onacityryresult
中初始化了NULL
值。