Chief Madog Chief Madog - 3 months ago 21
Android Question

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

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 ?

Answer

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