我有一个条形码扫描器应用程序,它具有以下布局......
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/scanner"
android:id="@+id/scan_barcode1"
android:layout_below="@+id/spinner7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="25dp"
android:onClick="openScanner1" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/scan_content"
android:layout_below="@+id/scan_barcode1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="@string/serial_number"
android:layout_alignRight="@+id/spinner7"
android:layout_alignEnd="@+id/spinner7" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/scanner"
android:id="@+id/scan_barcode2"
android:layout_below="@+id/scan_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="openScanner2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/scan_content2"
android:layout_below="@+id/scan_barcode2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="@string/serial_number"
android:layout_alignRight="@+id/scan_content"
android:layout_alignEnd="@+id/scan_content" />
我希望能够按下scan_barcode1并在editText scan_content中接收条形码的序列号,然后按scan_barcode2并在editText scan_content2中接收该条形码的序列号。当我按下两个按钮中的任何一个时,我的应用程序崩溃了。
public void openScanner1 (View view) {
IntentIntegrator scanIntegrator = new IntentIntegrator (this);
scanIntegrator.initiateScan();
startActivityForResult(scanIntegrator, 1);
}
public void openScanner2 (View view) {
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
startActivityForResult(scanIntegrator, 2);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null && requestCode == 1 && resultCode == RESULT_OK ) {
String scanContent = scanningResult.getContents();
contentTxt.setText(scanContent);
}
else if (scanningResult!=null && requestCode == 2 && resultCode == RESULT_OK) {
String scanContent = scanningResult.getContents();
contentTxt2.setText(scanContent);
}
else{
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
这是我的logcat ......
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1817)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1515)
at android.app.Activity.startActivityForResult(Activity.java:4026)
at android.app.Activity.startActivityForResult(Activity.java:3973)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
at com.example.gamezcua.etaksytemscop.BeforeBatteryInventory.openScanner1(BeforeBatteryInventory.java:521)
这是我的Android清单。
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/etak_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".EditExistingCloseOutPackage"
android:label="@string/title_activity_edit_existing_close_out_package" >
</activity>
<activity
android:name=".SiteInfo"
android:label="@string/title_activity_site_info" >
</activity>
<activity
android:name=".choice"
android:label="@string/title_activity_choice" >
</activity>
<activity
android:name=".NewOrExisting"
android:label="@string/title_activity_new_or_existing" >
</activity>
<activity
android:name=".NewBatteryInformation"
android:label="@string/title_activity_new_battery_information" >
</activity>
<activity
android:name=".ExistingBatteryInformation"
android:label="@string/title_activity_existing_battery_information" >
</activity>
<activity
android:name=".BatteryInstallationChecklist"
android:label="@string/title_activity_battery_installation_checklist" >
</activity>
<activity
android:name=".FinalInstallationChecklistBattery"
android:label="@string/title_activity_final_installation_checklist_battery" >
</activity>
<activity
android:name=".FinalSiteChecklist"
android:label="@string/title_activity_final_site_checklist" >
</activity>
<activity
android:name=".PhotoGallery"
android:label="@string/title_activity_photo_gallery" >
</activity>
<activity
android:name=".BeforeBatteryInventory"
android:label="@string/title_activity_before_battery_inventory" >
</activity>
<activity
android:name=".CompleteOnScopeWork"
android:label="@string/title_activity_complete_on_scope_work" >
</activity>
<activity
android:name=".JobType"
android:label="@string/title_activity_job_type" >
</activity>
<activity
android:name=".AfterInventory"
android:label="@string/title_activity_after_inventory" >
</activity>
<activity
android:name=".Both"
android:label="@string/title_activity_both" >
</activity>
</application>
更新了java文件
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult!=null) {
String scanContent = scanningResult.getContents();
editText32.setText(scanContent);
}
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
editText33.setText(scanContent);
}
else{
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}