NoClassDefFoundError during Facebook login

I made a demo of FBLogin using this tutorial

Code here //MainActivity.java

package com.example.fbdemo;
import android.os.Bundle;
import android.app.Activity;
import com.facebook.*;
import com.facebook.model.*;
import android.widget.TextView;
import android.content.Intent;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

      // start Facebook Login
    Session.openActiveSession(this, true, new Session.StatusCallback() {

      // callback when session changes state
      @Override
      public void call(Session session, SessionState state, Exception exception) {
        if (session.isOpened()) {

          // make request to the /me API
          Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

            // callback after Graph API response with user object
            @Override
            public void onCompleted(GraphUser user, Response response) {
              if (user != null) {
                TextView welcome = (TextView) findViewById(R.id.welcome);
                welcome.setText("Hello " + user.getName() + "!");
              }
            }
          });
        }
      }
    });

}

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

      

//main.xml

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

<TextView
    android:id="@+id/welcome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="TextView" />

</RelativeLayout>

      

//AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fbdemo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8" />

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.fbdemo.MainActivity"
        android:label="@string/app_name" >
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name="com.facebook.LoginActivity" />

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />
</application>

</manifest>

      

and this is a mistake.

02-14 16:40:43.245: E/AndroidRuntime(6300): FATAL EXCEPTION: main
02-14 16:40:43.245: E/AndroidRuntime(6300): java.lang.NoClassDefFoundError:  com.example.fbdemo.MainActivity$1
02-14 16:40:43.245: E/AndroidRuntime(6300):     at com.example.fbdemo.MainActivity.onCreate(MainActivity.java:18)
02-14 16:40:43.245: E/AndroidRuntime(6300):     at android.app.Activity.performCreate(Activity.java:4465)
02-14 16:40:43.245: E/AndroidRuntime(6300):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-14 16:40:43.245: E/AndroidRuntime(6300):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
02-14 16:40:43.245: E/AndroidRuntime(6300):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-14 16:40:43.245: E/AndroidRuntime(6300):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-14 16:40:43.245: E/AndroidRuntime(6300):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-14 16:40:43.245: E/AndroidRuntime(6300):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 16:40:43.245: E/AndroidRuntime(6300):     at android.os.Looper.loop(Looper.java:137)
02-14 16:40:43.245: E/AndroidRuntime(6300):     at android.app.ActivityThread.main(ActivityThread.java:4424)
02-14 16:40:43.245: E/AndroidRuntime(6300):     at java.lang.reflect.Method.invokeNative(Native Method)
02-14 16:40:43.245: E/AndroidRuntime(6300):     at java.lang.reflect.Method.invoke(Method.java:511)
02-14 16:40:43.245: E/AndroidRuntime(6300):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-14 16:40:43.245: E/AndroidRuntime(6300):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-14 16:40:43.245: E/AndroidRuntime(6300):     at dalvik.system.NativeStart.main(Native Method)

      

please give me a solution.

+3


source to share


1 answer


Android: name = ". MainActivity" is not a problem. This is just a shorthand notation for what you have in your manifest right now (which means: the MainActivity class in the default package, which is already com.example.fbdemo).

In fact, from your logging, you can see that it cannot be found in the MainActivity class, but com.example.fbdemo.MainActivity $ 1. $ 1 means something like (give me some rope) "the first anonymous inner class you have in your MainActivity". From your code, I find it safe to assume the class is Session.StatusCallback (), which you are new to your Session.openActiveSession () call.

I don't know this class, but is it from the library? If so, are you linking the .jar file of this library correctly? It should be in the "libs" directory of your project. In this case, it should be deployed along with your application. If you just link to it, it will only be available at compile time (hence the compiler won't complain), but the Android packaging step won't include it in the .apk file.



Hope it helps.

[edit] Of course I could be wrong about the $ 1 solution on Session.StatusCallback (). It can also be resolved by Request.GraphUserCallback (), which is just another of those "anonymous inner classes" in your code. You will have to play a little. Perhaps it will be both; -) [/ edit]

+1


source







All Articles