I am new to android application development. I am developing a simple barcode scanner application by using Zxing. I am using the latest ADT and I am developing under Eclipse.
Below are my codes:
package com.example.test_app2;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
public final static String SCAN_RESULT = "com.example.Test_App2.RESULT";
DisplayScanResultActivity disp = new DisplayScanResultActivity();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void scanBarcode(View view)
{
IntentIntegrator.initiateScan(this);
}
}
This is the MainActivity class.
package com.example.test_app2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
public class DisplayScanResultActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_scan_result);
// Show the Up button in the action bar.
//getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_display_scan_result, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE: {
if (resultCode != RESULT_CANCELED) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(
requestCode, resultCode, data);
if (scanResult != null) {
String upc = scanResult.getContents();
// put whatever you want to do with the code here
TextView tv = new TextView(this);
tv.setText(upc);
setContentView(tv);
}
}
break;
}
}
}
This is the activity which shows the scan result. But I am not able to understand how can I show the scan result.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test_app2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.test_app2.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="com.example.test_app2.DisplayScanResultActivity"
android:label="@string/title_activity_display_scan_result"
android:parentActivityName="com.example.Test_App2.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.Test_App2.MainActivity" />
</activity>
</application>
</manifest>
This is AndroidManifest.xml file.
When I run this project through emulator the scan button gets displayed. As soon as I click it, the application crashes. How can I solve this? Please help me find a solution.
This is a confused approach. You appear to be wanting to integrate with Barcode Scanner by Intent, but then you are sort of declaring our activity and namespace in your app, where it doesn't exist.
Throw all that away and just do this: http://code.google.com/p/zxing/wiki/ScanningViaIntent