The application crash on startup. Tried to pull out only the piece of code from the official : zxing-sample
So my activity_main.xml code is from the example such as
<?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>
Also in my java file 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);
}
}
So even after trying to pull out the code from sample i'm still doing something wrong and can't figure out what.
The application crash on startup so i think its a big problem which prevents me from testing the app.
Error SS:
Edit 1 : Asked for permission
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 {
}
}