我是android应用程序开发的新手。我正在使用 Zxing 开发一个简单的条形码扫描仪应用程序。我正在使用最新的 ADT,并且正在 Eclipse 下进行开发。
以下是我的代码:
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);
}
}
这是 MainActivity 类。
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;
}
}
}
这是显示扫描结果的活动。但我无法理解如何显示扫描结果。
<?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>
这是 AndroidManifest.xml 文件。
当我通过模拟器运行这个项目时,会显示扫描按钮。一旦我单击它,应用程序就会崩溃。我该如何解决这个问题?请帮我找到解决方案。