title: Integrating login with facebook in Android application
body: <p>Being a beginner, I am trying to implement facebook integration in my application. I got the app to run. However as son as i enter my credentials and it asks for my permission, that app crashes. I dont knw what is wrong. I followed a tutorial <a href="http://www.codeproject.com/Articles/820929/A-beginners-Guide-to-Social-Network-Integration-in" rel="nofollow">here</a> : </p>
  
  <p>Here is my main activity code :</p>
  
  <pre><code>package com.techfrk.facebooktesting;
  
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import android.content.Context;
  import android.content.SharedPreferences;
  import android.content.SharedPreferences.Editor;
  import android.os.Bundle;
  import android.support.v7.app.ActionBarActivity;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.Toast;
  import facebook.android.DialogError;
  import facebook.android.Facebook;
  import facebook.android.Facebook.DialogListener;
  import facebook.android.FacebookError;
  
  public class MainActivity extends ActionBarActivity 
  {
      private Facebook facebook;
      private static final String APP_ID = "758322840932665";
      private static final String[] PERMISSIONS = new String[] { "publish_stream","read_stream"};
      public static final String TOKEN = "access_token";
      public static final String EXPIRES = "expires_in";
      private static final String KEY = "facebook-credentials";
      Button bb;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) 
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          bb=(Button)findViewById(R.id.btnLoginNPublish);
          bb.setOnClickListener(new OnClickListener() 
          {
  
              @Override
              public void onClick(View v) 
              {
                  // TODO Auto-generated method stub
                  try
                  {
  
  
                  //loginToTwitter();
                   //MyTwitterLogin();
          /////////////// Facebook//////////////
                      facebook = new Facebook(APP_ID);
  
                      LoginToFacebook();
                 //     postToWall("Posting from my Eclipse project!!!");
                    //  fetchFacebookFriends();
                      ///////////////////////
                  }
                  catch(Exception ex)
                  {
                   Log.i("Exception:",ex.getMessage());
  
                  }
              }
          });
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) 
      {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) 
      {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
          if (id == R.id.action_settings) 
          {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      public boolean saveCredentials(Facebook facebook) 
      {
          Editor editor = getApplicationContext().getSharedPreferences(KEY,Context.MODE_PRIVATE).edit();
          editor.putString(TOKEN, facebook.getAccessToken());
          editor.putLong(EXPIRES, facebook.getAccessExpires());
          return editor.commit();
      }
  
      public boolean restoreCredentials(Facebook facebook) 
      {
          SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
          facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
          facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
          return facebook.isSessionValid();
      }
      public void LoginToFacebook() 
      {
          facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH,new LoginDialogListener());
      }
      public void postToWall(String msg) 
      {
          Log.d("Tests", "Testing graph API wall post");
           try 
           {
                  String response = facebook.request("me");
                  Bundle parameters = new Bundle();
                  parameters.putString("message", msg);
                  parameters.putString("description", "test test test");
                  response = facebook.request("me/feed", parameters,"POST");
                  Log.d("Tests", "got response: " + response);
                  if (response == null || response.equals("") || response.equals("false")|| response.contains("error")) 
                  {
                     Log.v("Error", response.toString());
                  }
           } 
           catch(Exception e) 
           {
               e.printStackTrace();
           }
      }
  
      public void fetchFacebookFriends()
      {
           try {
                  String response = facebook.request("me");
  
                  response = facebook.request("me/friends");
  
                  Log.d("Tests", "got response: " + response);
                  if (response == null || response.equals("") || response.equals("false")|| response.contains("error")) 
                  {
                     Log.v("Error", response.toString());
                  }
                  else
                  {
                          JSONObject jsonObject = new JSONObject(response);
                          try 
                          {
                              JSONArray array = jsonObject.getJSONArray("data");
                              for (int i = 0; i &lt; array.length(); i++) 
                              {
                                  JSONObject object = (JSONObject) array.get(i);
                                  Log.d( "id = "+object.get("id"),"Name = "+object.get("name"));
                              }
                          } 
                          catch (JSONException e) 
                          {
                              e.printStackTrace();
                          }
                  }
           }
           catch(Exception ex1)
           {
  
           }
  }
      public void fetchMyFacebookStatuses()
      {
           try {
                  String response = facebook.request("me");
  
                  response = facebook.request("me/statuses");
  
                  Log.d("Tests", "got response: " + response);
                  if (response == null || response.equals("") || response.equals("false")) 
                  {
                     Log.v("Error", response.toString());
                  }
                  else
                  {
                          JSONObject jsonObject = new JSONObject(response);
                          try 
                          {
                              JSONArray array = jsonObject.getJSONArray("data");
                              for (int i = 0; i &lt; array.length(); i++) 
                              {
                                  JSONObject object = (JSONObject) array.get(i);
                                  Log.d( "Message id = "+object.get("id"),"Message = "+object.get("message"));
                              }
                          } 
                          catch (Exception e) 
                          {
                              Log.i("Error in Statuses:",e.getMessage());
                          }
                  }
           }
           catch(Exception ex1)
           {
               Log.i("Error in Statuses:",ex1.getMessage());
           }
    }
  
      class LoginDialogListener implements DialogListener 
      {
          public void onComplete(Bundle values) 
          {
              saveCredentials(facebook);
              showToast( "Login Successfull in Facebook");
              //fetchFacebookFriends();
              //postToWall("Another post from Eclipse...");
               fetchMyFacebookStatuses();
              //fetchFriendsFacebookStatuses();    
          }
  
          @Override
          public void onFacebookError(FacebookError e) 
          {
              // TODO Auto-generated method stub
  
          }
  
          @Override
          public void onError(DialogError e) 
          {
              // TODO Auto-generated method stub
  
          }
  
          @Override
          public void onCancel() 
          {
              // TODO Auto-generated method stub
  
          }
  
      }
      private void showToast(String message) 
      {
             Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
      }
  }
  </code></pre>
  
  <p>Here is the Log Cat :</p>
  
  <pre><code>04-02 12:16:58.691: A/chromium(16016): [FATAL:jni_android.cc(269)] Check failed: false. Please include Java exception stack in crash report
  04-02 12:16:58.691: E/chromium(16016): ### WebView Version 40 (1808730-arm) (code 423501)
  04-02 12:16:58.691: A/libc(16016): Fatal signal 6 (SIGABRT), code -6 in tid 16016 (facebooktesting)
  </code></pre>