Ben Ben - 6 months ago 16
Android Question

Using facebook login in android app(no errors in logcat)

I am a beginner android programmer.I was trying to add a facebook login page into my android app.I made a class which is supposed to display the login page..
but when the line reaches setContentView(R.layout.fblogin); the program stops and there is no error in the logcat..so I cant understand the problem with the login page.Please help

Here is my Facebook login activity

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsLogger;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;


public class FbActivity extends Activity{
private CallbackManager callbackManager;
private TextView info;
private LoginButton loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.fblogin);

info = (TextView)findViewById(R.id.info);
loginButton = (LoginButton)findViewById(R.id.login_button);

FacebookSdk.sdkInitialize(getApplicationContext());
// AppEventsLogger.activateApp(this);
callbackManager = CallbackManager.Factory.create();


loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
info.setText(
"User ID: "
+ loginResult.getAccessToken().getUserId()
+ "\n" +
"Auth Token: "
+ loginResult.getAccessToken().getToken()
);
}

@Override
public void onCancel() {
info.setText("Login attempt canceled.");
}

@Override
public void onError(FacebookException e) {
info.setText("Login attempt failed.");
}
});

}catch (Exception e)
{
e.printStackTrace();
}

}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}


}


This is my login layout-fblogin.xml

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/info"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="18sp"
/>

<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>




And this is my Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pioneer.admin.digitalmenu" >

<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<!-- android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >-->

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

<activity
android:name="com.pioneer.admin.digitalmenu.LoginPage"
android:label="@string/title_activity_main" >
<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" />

<provider android:authorities="com.facebook.app.FacebookContentProvider1234"
android:name="com.facebook.FacebookContentProvider"
android:exported="true" />

<activity
android:name="com.pioneer.admin.digitalmenu.MainActivity"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="landscape"></activity>
<activity
android:name="com.pioneer.admin.digitalmenu.FbActivity"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="landscape"></activity>

<activity
android:name="com.pioneer.admin.digitalmenu.FeedBack"
android:theme="@style/AppTheme.NoActionBar"></activity>


<activity
android:name="com.pioneer.admin.digitalmenu.PrintDialogActivity"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>




Thanks..

Answer

try this code:

FacebookSdk.sdkInitialize(getApplicationContext()); put it before      

setContentView(R.layout.fblogin);
Comments