应用程序在启动时崩溃。 试图只从官方那里提取一段代码:zxing-sample
所以我的activity_main.xml代码来自示例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.design.widget.AppBarLayout
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:orientation="horizontal"
android:gravity="center"
android:background="#222"
android:layout_width="match_parent"
android:layout_height="150dp">
<Button
android:text="Koristiti kameru"
android:layout_weight="1"
android:onClick="toggleFlash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
也在我的Java文件MainActivity.java
中package com.example.myapplication;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class MainActivity extends BaseScannerActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
private boolean mFlash;
private static final String FLASH_STATE = "FLASH_STATE";
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.activity_main);
ViewGroup contentFrame = (ViewGroup) findViewById(R.id.content_frame);
mScannerView = new ZXingScannerView(this);
contentFrame.addView(mScannerView);
}
@Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this);
// You can optionally set aspect ratio tolerance level
// that is used in calculating the optimal Camera preview size
mScannerView.setAspectTolerance(0.2f);
mScannerView.startCamera();
mScannerView.setFlash(mFlash);
}
@Override
public void onPause() {
super.onPause();
mScannerView.stopCamera();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(FLASH_STATE, mFlash);
}
@Override
public void handleResult(Result rawResult) {
Toast.makeText(this, "Contents = " + rawResult.getText() +
", Format = " + rawResult.getBarcodeFormat().toString(), Toast.LENGTH_SHORT).show();
// Note:
// * Wait 2 seconds to resume the preview.
// * On older devices continuously stopping and resuming camera preview can result in freezing the app.
// * I don't know why this is the case but I don't have the time to figure out.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mScannerView.resumeCameraPreview(MainActivity.this);
}
}, 2000);
}
public void toggleFlash(View v) {
mFlash = !mFlash;
mScannerView.setFlash(mFlash);
}
}
因此,即使在尝试从示例中提取代码后,我仍然做错了事,无法弄清楚是什么。
应用程序在启动时崩溃,因此我认为这是一个很大的问题,导致我无法测试该应用程序。
错误SS:
编辑1:征求许可
public void launchActivity( ) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA}, ZXING_CAMERA_PERMISSION);
} else {
}
}
答案 0 :(得分:0)
在您的应用Gradle文件中,检查您的implementation 'com.android.support:design:28.0.0'
中是否有dependencies
答案 1 :(得分:0)
查看您的Logcat,它显示了问题所在。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class android.support.design.widget.AppBarLayout
...
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.widget.AppBarLayout" on path: DexPathList[[zip file "/data/app/com.example.myapplication-y9OZiywnTa-bCM9Op2luAw==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.myapplication-y9OZiywnTa-bCM9Op2luAw==/lib/arm, /system/lib, /vendor/lib]]
因此,这意味着它无法启动您的活动MainAcitivty
,因为它找不到类AppBarLayout
。您可能缺少依赖项。