title: Implemention of a Facebook Login Button Sdk 4 android
body: <p>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!!</p>
  
  <p><strong>MainActivity.java:</strong></p>
  
  <pre><code>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);
   }
  </code></pre>
  
  <p><strong>activity_main.xml:</strong></p>
  
  <pre><code>&lt;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"&gt;
  
  &lt;ImageView
      android:layout_width="300dp"
      android:layout_height="100dp"
      android:layout_gravity="center"
      android:src="@drawable/banner" /&gt;
  
  &lt;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" /&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>fragment_main.xml:</strong></p>
  
  <pre><code>&lt;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"&gt;
  
  
  &lt;TextView android:layout_width="match_parent" android:layout_height="match_parent"
      android:text="@string/hello_blank_fragment"
      android:gravity="center_horizontal" /&gt;
  
  &lt;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" /&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>and at last MainFragment.java:</strong></p>
  
  <pre><code>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&lt;LoginResult&gt;() {
          @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;
  }
  </code></pre>
  
  <p><strong>Edit: Android Manifest.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.moviere" &gt;
  &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  &lt;application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" &gt;
      &lt;activity
          android:name=".MainActivity"
          android:label="@string/app_name" &gt;
          &lt;meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;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" /&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p></p>