私は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ファイルです。
このプロジェクトをエミュレーターで実行すると、スキャンボタンが表示されます。クリックするとすぐにアプリケーションがクラッシュします。どうすればこれを解決できますか?解決策を見つけるのを手伝ってください。