1

Can't get Facebook login to work - the app always crashes as soon as I open it. Have been using this (https://developers.facebook.com/docs/android/scrumptious/authenticate) tutorial and redone it about 5 times, but still nothing.

SplashFragment.java

package com.example.braucamkopa;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SplashFragment extends Fragment {

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

SelectionFragment.java

package com.example.braucamkopa;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SelectionFragment extends Fragment {

private static final String TAG = "SelectionFragment";

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

MainLogin.java

package com.example.braucamkopa;

import com.facebook.Session;
import com.facebook.Session.StatusCallback;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class MainLogin extends FragmentActivity {

private static final int SPLASH = 0;
private static final int SELECTION = 1;
private static final int FRAGMENT_COUNT = SELECTION +1;

private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback = 
    new Session.StatusCallback() {
    @Override
    public void call(Session session, 
            SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
    }
};


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    uiHelper = new UiLifecycleHelper(this, callback);
    uiHelper.onCreate(savedInstanceState);

    setContentView(R.layout.mainlogin);

    FragmentManager fm = getSupportFragmentManager();
    fragments[SPLASH] = fm.findFragmentById(R.id.splashFragment);
    fragments[SELECTION] = fm.findFragmentById(R.id.selectionFragment);

    FragmentTransaction transaction = fm.beginTransaction();
    for(int i = 0; i < fragments.length; i++) {
        transaction.hide(fragments[i]);
    }
    transaction.commit();
}

private void showFragment(int fragmentIndex, boolean addToBackStack) {
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();
    for (int i = 0; i < fragments.length; i++) {
        if (i == fragmentIndex) {
            transaction.show(fragments[i]);
        } else {
            transaction.hide(fragments[i]);
        }
    }
    if (addToBackStack) {
        transaction.addToBackStack(null);
    }
    transaction.commit();
}

private boolean isResumed = false;

@Override
public void onResume() {
    super.onResume();
    uiHelper.onResume();
    isResumed = true;
}

@Override
public void onPause() {
    super.onPause();
    uiHelper.onPause();
    isResumed = false;
}

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

@Override
public void onDestroy() {
    super.onDestroy();
    uiHelper.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);
}

private void onSessionStateChange(Session session, SessionState state, Exception        exception) {
    // Only make changes if the activity is visible
    if (isResumed) {
        FragmentManager manager = getSupportFragmentManager();
        // Get the number of entries in the back stack
        int backStackSize = manager.getBackStackEntryCount();
        // Clear the back stack
        for (int i = 0; i < backStackSize; i++) {
            manager.popBackStack();
        }
        if (state.isOpened()) {
            // If the session state is open:
            // Show the authenticated fragment
            showFragment(SELECTION, false);
        } else if (state.isClosed()) {
            // If the session state is closed:
            // Show the login fragment
            showFragment(SPLASH, false);
        }
    }
}

protected void onResumeFragments() {

    Session session = Session.getActiveSession();

    if (session != null && session.isOpened()) {
        // if the session is already open,
        // try to show the selection fragment
        showFragment(SELECTION, false);
    } else {
        // otherwise present the splash screen
        // and ask the person to login.
        showFragment(SPLASH, false);
    }
}
}

Android Manifest

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

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

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

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SplashFragment"
        android:label="@string/app_name" >
    </activity>            
    <activity
        android:name=".SelectionFragment"
        android:label="@string/app_name" >
    </activity>
<meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />
</application>

</manifest>

LogCat

07-03 20:23:39.674: I/dalvikvm(25239): Could not find method android.support.v4.content.LocalBroadcastManager.getInstance, referenced from method com.facebook.UiLifecycleHelper.<init>
07-03 20:23:39.674: W/dalvikvm(25239): VFY: unable to resolve static method 835: Landroid/support/v4/content/LocalBroadcastManager;.getInstance (Landroid/content/Context;)Landroid/support/v4/content/LocalBroadcastManager;
07-03 20:23:39.674: D/dalvikvm(25239): VFY: replacing opcode 0x71 at 0x0019
07-03 20:23:39.674: I/dalvikvm(25239): Could not find method android.support.v4.content.LocalBroadcastManager.unregisterReceiver, referenced from method com.facebook.UiLifecycleHelper.onPause
07-03 20:23:39.674: W/dalvikvm(25239): VFY: unable to resolve virtual method 838: Landroid/support/v4/content/LocalBroadcastManager;.unregisterReceiver (Landroid/content/BroadcastReceiver;)V
07-03 20:23:39.674: D/dalvikvm(25239): VFY: replacing opcode 0x6e at 0x0004
07-03 20:23:39.674: I/dalvikvm(25239): Could not find method android.support.v4.content.LocalBroadcastManager.registerReceiver, referenced from method com.facebook.UiLifecycleHelper.onResume
07-03 20:23:39.674: W/dalvikvm(25239): VFY: unable to resolve virtual method 836: Landroid/support/v4/content/LocalBroadcastManager;.registerReceiver (Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;)V
07-03 20:23:39.674: D/dalvikvm(25239): VFY: replacing opcode 0x6e at 0x0032
07-03 20:23:39.674: D/AndroidRuntime(25239): Shutting down VM
07-03 20:23:39.674: W/dalvikvm(25239): threadid=1: thread exiting with uncaught exception (group=0x41abeba8)
07-03 20:23:39.684: E/AndroidRuntime(25239): FATAL EXCEPTION: main
07-03 20:23:39.684: E/AndroidRuntime(25239): Process: com.example.braucamkopa, PID: 25239
07-03 20:23:39.684: E/AndroidRuntime(25239): java.lang.NoClassDefFoundError: android.support.v4.content.LocalBroadcastManager
07-03 20:23:39.684: E/AndroidRuntime(25239):    at com.facebook.UiLifecycleHelper.<init>(UiLifecycleHelper.java:72)
07-03 20:23:39.684: E/AndroidRuntime(25239):    at com.example.braucamkopa.MainLogin.onCreate(MainLogin.java:33)
07-03 20:23:39.684: E/AndroidRuntime(25239):    at android.app.Activity.performCreate(Activity.java:5231)
07-03 20:23:39.684: E/AndroidRuntime(25239):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-03 20:23:39.684: E/AndroidRuntime(25239):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
07-03 20:23:39.684: E/AndroidRuntime(25239):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
07-03 20:23:39.684: E/AndroidRuntime(25239):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-03 20:23:39.684: E/AndroidRuntime(25239):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-03 20:23:39.684: E/AndroidRuntime(25239):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-03 20:23:39.684: E/AndroidRuntime(25239):    at android.os.Looper.loop(Looper.java:136)
07-03 20:23:39.684: E/AndroidRuntime(25239):    at android.app.ActivityThread.main(ActivityThread.java:5001)
07-03 20:23:39.684: E/AndroidRuntime(25239):    at java.lang.reflect.Method.invokeNative(Native Method)
07-03 20:23:39.684: E/AndroidRuntime(25239):    at java.lang.reflect.Method.invoke(Method.java:515)
07-03 20:23:39.684: E/AndroidRuntime(25239):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-03 20:23:39.684: E/AndroidRuntime(25239):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-03 20:23:39.684: E/AndroidRuntime(25239):    at dalvik.system.NativeStart.main(Native Method)
07-03 20:27:55.754: I/Process(25937): Sending signal. PID: 25937 SIG: 9
07-03 20:34:58.184: I/Process(26488): Sending signal. PID: 26488 SIG: 9

It seems to me that i've tried everything, so now I must ask you guys.. Please help me out, i'm loosing my mind here!

FIXED Fixed this error by fixing dependencies (3 different versions of android_support-v4.jar - wtf to that!) after that fixed build path by deleting the duplicates and finally clean/rebuild. Thanks for pointing in the right direction.

user3783123
  • 544
  • 3
  • 15
  • Check versions of phone/simulator where you run it with version you develop it for. Also this looks a bit similar to: http://stackoverflow.com/questions/6168841/unable-to-resolve-superclass-error-when-referencing-jar-library – jakub.petr Jul 03 '14 at 17:07
  • Took a look at this and I have my libs folder with an s and Private Libraries are checked for the project, and support .jar has been added. – user3783123 Jul 03 '14 at 17:47

2 Answers2

0

Your Activity exists and the path is correct. Your FragmentActivity is probably imported from the support package, which I guess your IDE didn't include.

To include it and to fix your problem, right-click your project and select properties. Then, go to Build Path and:

  1. Check if Android Private Libraries is checked.
  2. Check if the support .jar is in your Build Path.

Finally, clean/rebuild your project and you should be good to go.

Aashir
  • 2,621
  • 4
  • 21
  • 37
  • Libraries and support .jar were OK. I did a clean and rebuild, and now LogCat is giving some other error. Just updated the LogCat. – user3783123 Jul 03 '14 at 17:38
0

I fixed with Build -> Clean Project in Android Studio

Kiryl Bielašeŭski
  • 2,663
  • 2
  • 28
  • 40