简体   繁体   中英

Getting java.lang.NoClassDefFoundError in android with Eclipse

I am getting the following Logcat information when I click a button on my app:

06-19 11:24:45.308: E/AndroidRuntime(11498): FATAL EXCEPTION: main
06-19 11:24:45.308: E/AndroidRuntime(11498): java.lang.IllegalStateException: Could not execute method of the activity
06-19 11:24:45.308: E/AndroidRuntime(11498):    at android.view.View$1.onClick(View.java:3674)
06-19 11:24:45.308: E/AndroidRuntime(11498):    at android.view.View.performClick(View.java:4198)
06-19 11:24:45.308: E/AndroidRuntime(11498):    at android.view.View$PerformClick.run(View.java:17164)
06-19 11:24:45.308: E/AndroidRuntime(11498):    at android.os.Handler.handleCallback(Handler.java:615)
06-19 11:24:45.308: E/AndroidRuntime(11498):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 11:24:45.308: E/AndroidRuntime(11498):    at android.os.Looper.loop(Looper.java:137)
06-19 11:24:45.308: E/AndroidRuntime(11498):    at android.app.ActivityThread.main(ActivityThread.java:4918)
06-19 11:24:45.308: E/AndroidRuntime(11498):    at java.lang.reflect.Method.invokeNative(Native Method)
06-19 11:24:45.308: E/AndroidRuntime(11498):    at java.lang.reflect.Method.invoke(Method.java:511)
06-19 11:24:45.308: E/AndroidRuntime(11498):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
06-19 11:24:45.308: E/AndroidRuntime(11498):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
06-19 11:24:45.308: E/AndroidRuntime(11498):    at dalvik.system.NativeStart.main(Native Method)
06-19 11:24:45.308: E/AndroidRuntime(11498): Caused by: java.lang.reflect.InvocationTargetException
06-19 11:24:45.308: E/AndroidRuntime(11498):    at java.lang.reflect.Method.invokeNative(Native Method)
06-19 11:24:45.308: E/AndroidRuntime(11498):    at java.lang.reflect.Method.invoke(Method.java:511)
06-19 11:24:45.308: E/AndroidRuntime(11498):    at android.view.View$1.onClick(View.java:3669)
06-19 11:24:45.308: E/AndroidRuntime(11498):    ... 11 more
06-19 11:24:45.308: E/AndroidRuntime(11498): Caused by: java.lang.NoClassDefFoundError: com.facebook.android.MainActivity$UpdateStatusListener
06-19 11:24:45.308: E/AndroidRuntime(11498):    at com.facebook.android.MainActivity.triggerDialog(MainActivity.java:55)
06-19 11:24:45.308: E/AndroidRuntime(11498):    at com.facebook.android.MainActivity.firstClicked(MainActivity.java:36)
06-19 11:24:45.308: E/AndroidRuntime(11498):    ... 14 more

Here is how I have my classes set up, with only the relevant methods and info displayed here:

public class MainActivity extends Activity {

    //triggered when the button is clicked
    public void firstClicked(View view)
    {
        triggerDialog();
    }

    public void triggerDialog()
    {
                //assume for the sake of this post that this string array has 4 strings
            String[] offer_details = postOffer.getDetails();
            Bundle params = new Bundle();
                params.putString("caption", getString(R.string.app_name));  //Hackbook for Android

                Utility.mFacebook.dialog(MainActivity.this, "feed", params, new UpdateStatusListener());
    }

    public class UpdateStatusListener extends BaseDialogListener {

        @Override
        public void onComplete(Bundle values) {
            Log.i("wbbug","Status post complete.");
            final String postId = values.getString("post_id");
            if (postId != null) {
                Toast toast = Toast.makeText(getApplicationContext(), "Facebook status update successful", Toast.LENGTH_LONG);
                toast.show();
            } else {
                Toast toast = Toast.makeText(getApplicationContext(), "No wall post made",
                        Toast.LENGTH_LONG);
                toast.show();
            }
        }

    }


}

This is all directly following an example from the Facebook SDK, and the example is working with this basic code. Why is it that it cannot find the class UpdateStatusListener()? Thanks!

I was trying to get started using Facebook's SDK for Android with Eclipse and couldn't get it to work. After trying different things here is the solution that consistently works:

1) Import (File->Import->Existing Android Code Into Workspace) just the Facebook SDK folder alone (PATH\\facebook-android-sdk-3.0.1). (Do not check the copy to workplace)

2) Import (ie PATH\\facebook-android-sdk-3.0.1\\samples\\ProfilePictureSample) just one of the sample projects (for now). I will be using ProfilePictureSample as an example

As you can see, Eclipse throws errors saying that it doesn't know what FragmentActivity in ProfilePictureSampleActivity. FragmentActivity is part of the android support library. If you take a look at the package explorer, there is not a libs folder or any reference to the android support lirbary; It is on the FacebookSDK library. We need to tell Eclipse to export it.

4) Right-Click on the FacebookSDK library then click on properties. On the left menu go to Java Build Path. Then under the Order and Export tab check Android Private Libraries (you can also click on the android-supportv4-jar instead).

5) Project->Clean

Now for some reason (maybe somebody can elaborate on this), the sample project also needs to export the android support library.

6) Right-Click on the sample project (ie ProfilePictureSample) then click on properties. On the left menu go to Java Build Path. Then under the Order and Export tab check Android Private Libraries.

7) Project->Clean

Hopefully this helps!!

This exception usually means that your vm can't find the necessary libraries. It seems odd that it happens at runtime though. Try checking if your libraries are all there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM