Android库项目中的java.lang.ClassNotFoundException

时间:2015-10-02 06:10:07

标签: java android android-studio

我有2个项目,第一个是主项目,第二个是一个库项目。图书馆项目有自定义警告对话框,带有布局xml文件。但是当我将库导入主项目并调用自定义警报时,它会给出java.lang.ClassNotFoundException:

Main Project提醒对话框

public class MainFragment extends Fragment {

private TextView textView;
private CallbackManager callbackManager;
private AccessTokenTracker accessTokenTracker;
private ProfileTracker profileTracker;

private ServiceCall serviceCall;
private SendFbData sendFbData;

Activity activity;
String facebook_id = "";
String email =  "";
String first_name = "";
String last_name = "";
String location = "";
String os = "";


private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        AccessToken accessToken = loginResult.getAccessToken();
        Profile profile = Profile.getCurrentProfile();

        GraphRequest request = GraphRequest.newMeRequest(
                loginResult.getAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(
                            JSONObject object,
                            GraphResponse response) {
                        // Application code
                        Log.v("LoginActivity", response.toString());

                        try {
                           sendFbData.getUserPermission(getActivity());

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,last_name,first_name,email,gender,location,hometown");
        request.setParameters(parameters);
        request.executeAsync();



       // displayWelcomeMessage(profile);
   }

    @Override
    public void onCancel() {

    }

    @Override
    public void onError(FacebookException e) {

    }
};

public MainFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getActivity());


    callbackManager = CallbackManager.Factory.create();
    AccessTokenTracker accessTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken old, AccessToken newTocken) {

        }
    };

    ProfileTracker profileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(Profile old, Profile newProfile) {

        }
    };
    accessTokenTracker.startTracking();
    profileTracker.startTracking();

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_main, container, false);

    serviceCall = new ServiceCall();
    sendFbData = new SendFbData();

    textView = (TextView) view.findViewById(R.id.text);
    LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
    loginButton.setFragment(this);
    loginButton.setReadPermissions("user_friends");
    loginButton.setReadPermissions("public_profile");
    loginButton.setReadPermissions("email");
    loginButton.registerCallback(callbackManager, mCallback);

    return view;
}



@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

}

@Override
public void onResume() {
    super.onResume();
    Profile profile = Profile.getCurrentProfile();
    displayWelcomeMessage(profile);
}

@Override
public void onStop() {
    super.onStop();
    accessTokenTracker.stopTracking();
    profileTracker.stopTracking();
}

private void displayWelcomeMessage(Profile profile){

    if(profile != null){
        textView.setText("Welcome " + profile.getName());

    }

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    callbackManager.onActivityResult(requestCode, resultCode, data);

}
}

Library Project    - custom_alert_dialog.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background"
    android:orientation="horizontal"
    android:weightSum="1">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal">


        <Button
            android:background="@null"
            android:id="@+id/btn_no"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="1"

            />

        <Button
            android:background="@null"
            android:id="@+id/btn_yes"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="1"


            />
    </LinearLayout>
</LinearLayout>

  • SendFbData.class

     public class SendFbData extends ActionBarActivity {
    
     public SendFbData(){
    
     }
    
     @Override
     public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_get_user_info);
    
     }
    
     public void getUserPermission(final Activity activity, final    ServiceCall serviceCall){
    
     final Dialog dialog = new Dialog(activity);
    dialog.setContentView(R.layout.custom_alert_dialog);
    
    Button btn_no = (Button) dialog.findViewById(R.id.btn_no);
    Button btn_yes = (Button) dialog.findViewById(R.id.btn_yes);
    
    btn_no.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();
        }
    });
    
    btn_yes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new ServiceCall(activity, serviceCall).execute();
        }
    });
    
    dialog.show();
    }
    }
    

Log cat

Process: android.wake.testfbloginwithlibrary, PID: 7253
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/wake/androidlib/R$layout;
        at com.wake.androidlib.SendFbData.getUserPermission(SendFbData.java:65)
        at android.wake.testfbloginwithlibrary.MainFragment$1$1.onCompleted(MainFragment.java:90)
        at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:304)
        at com.facebook.GraphRequest$5.run(GraphRequest.java:1368)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.wake.androidlib.R$layout" on path: DexPathList[[zip file "/data/app/android.wake.testfbloginwithlibrary-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
        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.wake.androidlib.SendFbData.getUserPermission(SendFbData.java:65)
            at android.wake.testfbloginwithlibrary.MainFragment$1$1.onCompleted(MainFragment.java:90)
            at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:304)
            at com.facebook.GraphRequest$5.run(GraphRequest.java:1368)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Suppressed: java.lang.ClassNotFoundException: com.wake.androidlib.R$layout
        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)
        ... 13 more
 Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {

        minSdkVersion 9
        targetSdkVersion 22

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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

//task to delete the old jar
task deleteOldJar(type: Delete){
    delete 'release/WKNS.jar'
}

//task to export contents as jar
task exportJar(type: Copy){
    from('build/intermediates/bundles/release/')
    into('release/')
    include('classes.jar')
    //rename the jar
    rename('classes.jar', 'WKNS.jar')
}

exportJar.dependsOn(deleteOldJar, build)

1 个答案:

答案 0 :(得分:1)

尝试以下步骤,因为它对我有用。

步骤1:右键单击您的项目并选择构建路径,然后选择配置构建路径。

第2步:选择android并删除导入的Library Project。并申请。

重新启动IDE并尝试再次导入它。它对我有用。希望它对你也有用.. 谢谢