0

So, I'm trying to implement a login Button using Fragment. The documentation on facebook site is just a little bit confusing. My app crashes even on the start. There is no error in build. Here is my code.. If someone can help me I'd be grateful!!

MainActivity.java:

public class MainActivity extends FragmentActivity {


    private CallbackManager callbackManager;
   // private Fragment mainfrag=new Fragment();
    private AccessTokenTracker accessTokenTracker;
    private boolean isResumed = false;


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


        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        accessTokenTracker = new AccessTokenTracker() {
           @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {

            }
        };

        setContentView(R.layout.activity_main);
 }

activity_main.xml:

<LinearLayout 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:background="@drawable/bg"
android:orientation="vertical"
android:weightSum="1">

<ImageView
    android:layout_width="300dp"
    android:layout_height="100dp"
    android:layout_gravity="center"
    android:src="@drawable/banner" />

<fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="com.moviere.MainFragment"
    android:id="@+id/fragment"
    android:layout_gravity="center_horizontal"
    android:layout_weight="0.17" />

fragment_main.xml:

<FrameLayout 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" tools:context="com.moviere.MainFragment">


<TextView android:layout_width="match_parent" android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment"
    android:gravity="center_horizontal" />

<com.facebook.widget.LoginButton
    android:id="@+id/login_button"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|top"
    android:layout_marginTop="30dp" />

and at last MainFragment.java:

public class MainFragment extends Fragment {

private CallbackManager callbackManager;
private LoginButton loginButton;

public MainFragment() {
    // Required empty public constructor
}

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

    loginButton = (LoginButton) view.findViewById(R.id.login_button);
    loginButton.setReadPermissions("user_friends");
    // If using in a fragment
    loginButton.setFragment(this);
    // Other app specific specialization
// Callback registration
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Toast.makeText(getActivity(),"Success",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel() {
            Toast.makeText(getActivity(),"fail",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(FacebookException exception) {
            Toast.makeText(getActivity(),"error", Toast.LENGTH_SHORT).show();
        }
    });
    return view;
}

Edit: Android Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.moviere" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.facebook.FacebookActivity"
        android:configChanges=
            "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"
        android:label="@string/app_name" />
</application>

linous
  • 263
  • 1
  • 6
  • 17
  • Try to look at your LogCat log, instead of build log. You may find error message there. Also, try to request "public_profile" permission instead of a "user_friends". – VadymVL Mar 30 '15 at 09:14
  • The logcat shows only this: threadid=1: thread exiting with uncaught exception (group=0x41c96c50) . I googled it and it says maybe I'm using a custom package and declare it at Android Manifest. Except for facebook,I',m not using something else. – linous Mar 30 '15 at 12:04
  • Have you declared facebook SDK in [Manifest](https://developers.facebook.com/docs/android/getting-started#androidstudio)? – VadymVL Mar 30 '15 at 12:06
  • yes, I attached the manifest in my original post as well. – linous Mar 30 '15 at 12:43
  • What i can see, that you have added `meta-data` inside your activity, instead of an `application` tag. From the link above: "5. Add a meta-data element to the application element:" – VadymVL Mar 30 '15 at 12:55
  • You're right,I added it inside but the exception is still the same. – linous Mar 30 '15 at 13:16
  • It's hard to say what's going wrong, without certain code error. What I can say, is that this last 4.0 SDK for Android is pretty buggy, I have some issues with it too. – VadymVL Mar 30 '15 at 13:27
  • This video helped me to create Facebook login button for my app < https://www.youtube.com/watch?v=myWu-q8Q2NA > hope it might help anyone else :) ... – Anitha May 19 '15 at 05:41

4 Answers4

2

I also meet that problem when I study facebook 4.0,and I try again with android studio,I found that app will crash if

loginButton.registerCallback(callbackManager.....

callbackManager is null,so I put

callbackManager = CallbackManager.Factory.create();

before

loginButton.registerCallback(callbackManager.....

then run it,it work,I hope it can help you

my simple demo : https://github.com/oliguo/android-facebook

Oliver Guo
  • 166
  • 9
1

You have added everything required to integrate Facebook SDK but there is a little bit problem in positioning of code.

You are using LoginButton in fragment, but initializing FacebookSdk with instance of Activity. You need not do anything in activity, just update the code of your fragment like this:

...
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
    FacebookSdk.sdkInitialize(this);
    callbackManager = CallbackManager.Factory.create();        
    View view = inflater.inflate(R.layout.fragment_main, container, false);

    loginButton = (LoginButton) view.findViewById(R.id.login_button);
    loginButton.setReadPermissions("user_friends");
    loginButton.setFragment(this);

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
    ...
    }
}

@Override
... onActivityResult(...) {
    super.onActivityResult(...);
    callbackManager.onActivityResult(...);
}

Also, start the ProfileTracker/AccessTokenTracker from onSuccess() method of login callback, stop in onCancel() and onDestroy().

Ankit Bansal
  • 1,801
  • 17
  • 34
1

maybe you are missing the facebook attribute "xmlns:facebook="http://schemas.android.com/apk/res-auto" in fragment_main.xml.

<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 
xmlns:facebook="http://schemas.android.com/apk/res-auto
android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.moviere.MainFragment">

If it does not work i just answered a question using the LoginButton.

Answered question

Community
  • 1
  • 1
schwertfisch
  • 4,549
  • 1
  • 19
  • 32
0

I have answered this question in this post please check the answer accordingly and compare to your code.

Community
  • 1
  • 1