4

i'm trying to launch an Activity with a Fragment inside that is doing a QR scan, i'm using this library for that > https://code.google.com/archive/p/barcodefraglibv2/

I do everything by instructions but when i run the app it throws away an exception :

Failed resolution of: Lcom/abhi/barcode/frag/libv2/R$string

what am i doing wrong ?

here is my MainActivity:

package apps.radwin.zxingprojectfragmentsthree;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.abhi.barcode.frag.libv2.BarcodeFragment;
import com.abhi.barcode.frag.libv2.IScanResultHandler;
import com.abhi.barcode.frag.libv2.ScanResult;
import com.google.zxing.BarcodeFormat;

import java.util.EnumSet;

public class MainActivity extends FragmentActivity implements IScanResultHandler {

        BarcodeFragment fragment;
        Button btn;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            fragment = (BarcodeFragment)getSupportFragmentManager().findFragmentById(R.id.sample);
            fragment.setScanResultHandler(this);
            btn = ((Button)findViewById(R.id.scan));
            btn.setEnabled(false);

            // Support for adding decoding type
            fragment.setDecodeFor(EnumSet.of(BarcodeFormat.QR_CODE));
        }

        @Override
        public void scanResult(ScanResult result) {
            btn.setEnabled(true);
            Toast.makeText(this, result.getRawResult().getText(), Toast.LENGTH_LONG).show();
        }

        public void scanAgain(View v){
            fragment.restart();
        }
    }

here is my build.gradle file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "apps.radwin.zxingprojectfragmentsthree"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile files('libs/barcodelib.jar')
    compile files('libs/core.jar')
}

My Activity XML >

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity">

        <fragment
            android:id="@+id/sample"
            android:name="com.abhi.barcode.frag.libv2.BarcodeFragment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/scan"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />

        <Button
            android:id="@+id/scan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:onClick="scanAgain"
            android:text="Re-Scan" />
</RelativeLayout>

my exception is:

FATAL EXCEPTION: main

Process: apps.radwin.zxingprojectfragmentsthree, PID: 11692
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/abhi/barcode/frag/libv2/R$string;
at com.abhi.barcode.frag.libv2.BarcodeFragment.displayFrameworkBugMessageAndExit(BarcodeFragment.java:345)
at com.abhi.barcode.frag.libv2.BarcodeFragment.initCamera(BarcodeFragment.java:338)
at com.abhi.barcode.frag.libv2.BarcodeFragment.surfaceCreated(BarcodeFragment.java:212)
at android.view.SurfaceView.updateWindow(SurfaceView.java:712)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:209)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:1014)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2510)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
at android.view.Choreographer.doCallbacks(Choreographer.java:695)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.abhi.barcode.frag.libv2.R$string" on path: DexPathList[[zip file "/data/app/apps.radwin.zxingprojectfragmentsthree-1/base.apk"],nativeLibraryDirectories=[/data/app/apps.radwin.zxingprojectfragmentsthree-1/lib/arm64, /vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at com.abhi.barcode.frag.libv2.BarcodeFragment.displayFrameworkBugMessageAndExit(BarcodeFragment.java:345) 
at com.abhi.barcode.frag.libv2.BarcodeFragment.initCamera(BarcodeFragment.java:338) 
at com.abhi.barcode.frag.libv2.BarcodeFragment.surfaceCreated(BarcodeFragment.java:212) 
at android.view.SurfaceView.updateWindow(SurfaceView.java:712) 
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:209) 
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:1014) 
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2510) 
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437) 
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397) 
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920) 
at android.view.Choreographer.doCallbacks(Choreographer.java:695) 
at android.view.Choreographer.doFrame(Choreographer.java:631) 
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Suppressed: java.lang.ClassNotFoundException: com.abhi.barcode.frag.libv2.R$string
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 21 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

in my app/libs folder, i have two files, barcodelib.jar, and code.jar, i pressed in my android studio on the barcodelib.jar file right click, then add as a library, i cannot press that on the core.jar as well, it doesn't give the option.

can you help me by saying what am i doing wrong ?

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
Chief Madog
  • 1,738
  • 4
  • 28
  • 55

2 Answers2

1

As of API level 23 and higher camera permission needs to be requested at run time

Change your onCreate code to:

        if (PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA))
        { 

            // asks primission to use the devices camera
            fragment = (BarcodeFragment)getSupportFragmentManager().findFragmentById(R.id.sample);
            fragment.setScanResultHandler(this);
            btn = ((Button)findViewById(R.id.scan));
            btn.setEnabled(false);

            // Support for adding decoding type
            fragment.setDecodeFor(EnumSet.of(BarcodeFormat.QR_CODE));

        } 
        else 
        {
            requestWritePermission(MainActivity.this);
        }

and add this function to ask the permission from the user:

    private static void requestWritePermission(final Context context) 
    {
        if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.CAMERA)) 
        {
            new AlertDialog.Builder(context).setMessage("This app needs permission to use The phone Camera in order to activate the Scanner")
                    .setPositiveButton("Allow", new DialogInterface.OnClickListener() {
                        @Override public void onClick(DialogInterface dialog, int which) 
                        { ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CAMERA}, 1);}
                    }).show();
        } 
        else 
        {
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CAMERA}, 1);
        }
    }

better to use this before going into the Fragment Activity add these lines and try again. Also for more input read the flowing articale:

https://developer.android.com/training/permissions/requesting.html

Chief Madog
  • 1,738
  • 4
  • 28
  • 55
deerdeer
  • 108
  • 7
0

First in your module level build.gradle , you have added compile the Jar files multiple times... Remove those and only keep:

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'  
}

fileTree already compiles all the jar files inside your app/libs

Now remove the libraries from your Android studio, the same way you added them.

Now right click your project-> show in explorer....

Find the path to your libs and put the two Jar files barcodelib.jar and core.jar

Now register the Dependencies in your gradle

1)Open file menu-> click on project structure

2)Select app here

3)click on dependencies tab shown

4)click on the plus sign in the top right corner

5)Select File Dependency and find your JAR from the libs folder... Do the same for both the JAR files

6)Click OK and APPLY

7)Select "Sync project with gradle files" button on toolbar.

8)Invalidate cache and restart

Source for more details: http://www.theappguruz.com/blog/external-jar-dependency-android-studio

This should be enough... If not, please post the rest of the Error log which is missing in the question (21 more lines...)

Kushan
  • 5,855
  • 3
  • 31
  • 45
  • thnx allot, when i register the Dependencies in my gradle it audomaticaly adds, the two lines i removed previosely from the gradle, i cannot see the 21 more lines, it stops there, and i cannot press on it, as you can see the next line says "no stack trace available" . still it didn't help. i still get the same problem – Chief Madog Aug 11 '16 at 06:36
  • Hey, keep the core.jar as a File dependency and add barcodelib.jar as a Library dependency.... instead of file dependency... – Kushan Aug 11 '16 at 09:42
  • iv'e tried that and it's doing the exact same Error, can you maek that library to work at your side ? – Chief Madog Aug 14 '16 at 11:13