这似乎与许多现有的类似问题重复。但我几乎阅读了所有的讨论,没有人在帮助。我的目标是将zxing库嵌入到我的应用程序中,并扫描QRCodes而无需引用外部应用程序。我目前的平台目标是android 4.2。
我一遍又一遍地完成了以下步骤:
当我运行应用程序时,在最终获得异常之前,我在dalvikvm中看到了很多资源解决问题。我的理解/测试zxing库在编译为库时无法访问本地资源文件或静态变量。它终于爆发了NoClassDefFoundError试图找到CaptureActivity的RelativeLayout。
以下是我收到的错误类型的简化列表。
01-04 23:30:37.851: W/dalvikvm(9804): VFY: unable to resolve static field 1711 (decode_succeeded) in Lcom/google/zxing/client/android/R$id;
01-04 23:30:37.851: D/dalvikvm(9804): VFY: replacing opcode 0x60 at 0x0011
01-04 23:30:37.851: W/dalvikvm(9804): VFY: unable to resolve static field 1761 (app_name) in Lcom/google/zxing/client/android/R$string;
01-04 23:30:37.851: D/dalvikvm(9804): VFY: replacing opcode 0x60 at 0x0005
..................(more of similar)
01-04 23:30:37.875: I/dalvikvm(9804): DexOpt: unable to optimize static field ref 0x0702 at 0x0e in Lcom/google/zxing/client/android CaptureActivity;.displayFrameworkBugMessageAndExit
01-04 23:30:37.875: I/dalvikvm(9804): DexOpt: unable to optimize static field ref 0x06eb at 0x17 in Lcom/google/zxing/client/android/CaptureActivity;.displayFrameworkBugMessageAndExit
..................(more of similar)
01-04 23:30:43.617: E/AndroidRuntime(9804): FATAL EXCEPTION: main
01-04 23:30:43.617: E/AndroidRuntime(9804): java.lang.NoClassDefFoundError: com.google.zxing.client.android.R$layout
01-04 23:30:43.617: E/AndroidRuntime(9804): at com.google.zxing.client.android.CaptureActivity.onCreate(CaptureActivity.java:134)
..................(rest of exception stack)
package my.android.application;
import my.android.ordertaker.R;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.Toast;
import com.google.zxing.Result;
import com.google.zxing.client.android.CaptureActivity;
public class ScannerActivity extends CaptureActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
}
@Override
public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor) {
Toast.makeText(this.getApplicationContext(), "Scanned code " + rawResult.getText(), Toast.LENGTH_LONG);
}
}
这是AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.android.ordertaker"
android:versionCode="3"
android:versionName="3.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- uses-permission android:name="android.permission.ACCESS_WIFI_STATE" / -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<!-- android:theme="@style/AppTheme" -->
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/CustomTheme" >
<activity
android:name="my.android.application.ScannerActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- service android:name="com.commonsware.cwac.updater.UpdateService"/ -->
<!-- receiver android:name="com.commonsware.cwac.updater.WakefulReceiver"/ -->
<receiver android:name="my.android.io.NetworkStatusListener" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" >
</action>
</intent-filter>
</receiver>
</application>
</manifest>
我还尝试了什么:我通过修改它来玩zxing / android源码(captureactivity.jar),试图用直接值(硬编码)删除R.id引用,拉出一些对我自己的项目的类,将一些源访问器更改为公共等等,我取得了一些成功,我甚至让相机工作,但我最终无法管理它,范围也是如此很难处理,所以我又回来了。
根据我的经验,问题似乎来自生成的库(captureactivity.jar)无法引用它的内部资源,但我可能错了。我的问题似乎比技术更基础。我做错了什么?
答案 0 :(得分:0)
首先将ZXing项目作为库项目来处理资源问题。正如@SeanOwen所指出的那样,如果您打算将其与适当的归因一起分发,则需要更改UI。