Question

I am using Facebook SDK in my app but my app crashes after i click on the login button. But the time is not fixed. Sometimes it crashes after i have logged in, sometimes at the instant i click on login button, sometimes in between when i am filling out the details for login.

Also can anybody tell me why the shared preferences are not working.

MainActivity.this

import com.facebook.android.FacebookError;
import com.facebook.android.Facebook.DialogListener;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
    Facebook fb;
    ImageView login;
    SharedPreferences sp;
    String APP_ID = "123456";
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fb = new Facebook(APP_ID);
        sp = getPreferences(MODE_PRIVATE);
        String access_token = sp.getString("access_token", null);
        long expires = sp.getLong("expires", 0);
        if (access_token != null) {
            fb.setAccessToken(access_token);
        }
        if (expires != 0) {
            fb.setAccessExpires(expires);
        }
        login = (ImageView) findViewById(R.id.imageView1);
        login.setOnClickListener(this);
    }

    @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 void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId() == R.id.imageView1) {
            fbLogin();
        }
    }

    private void fbLogin() {
        if (fb.isSessionValid()) {
            Intent i = new Intent(MainActivity.this, com.example.test.Register.class);
            startActivity(i);
        } else {
            fb.authorize(MainActivity.this, new String[] { "email" },
                    new DialogListener() {

                        @Override
                        public void onFacebookError(FacebookError e) {
                            // TODO Auto-generated method stub
                            e.printStackTrace();
                            Toast.makeText(MainActivity.this, "fbError",Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onError(DialogError e) {
                            // TODO Auto-generated method stub
                            e.printStackTrace();
                            Toast.makeText(MainActivity.this, "onError",Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onComplete(Bundle values) {
                            // TODO Auto-generated method stub
                            Editor editor = sp.edit();
                            editor.putString("access_token",fb.getAccessToken());
                            editor.putLong("access_expires",fb.getAccessExpires());
                            editor.commit();
                            Intent i = new Intent(MainActivity.this, com.example.test.Register.class);
                            startActivityForResult(i, RESULT_OK);
                        }

                        @Override
                        public void onCancel() {
                            // TODO Auto-generated method stub
                            Toast.makeText(MainActivity.this, "onCancel",Toast.LENGTH_SHORT).show();
                        }
                    });
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        fb.authorizeCallback(requestCode, resultCode, data);
    }

}

So please can anybody help me to identify :

  1. Why the app is crashing at any time after i click on the the login button.
  2. Why the shared preferences is not working.

Logcat

03-31 00:31:47.459: E/AndroidRuntime(5121): FATAL EXCEPTION: Timer-0
03-31 00:31:47.459: E/AndroidRuntime(5121): java.lang.NoClassDefFoundError: android.support.v4.content.LocalBroadcastManager
03-31 00:31:47.459: E/AndroidRuntime(5121):     at com.facebook.AppEventsLogger.flushAndWait(AppEventsLogger.java:760)
03-31 00:31:47.459: E/AndroidRuntime(5121):     at com.facebook.AppEventsLogger.access$1(AppEventsLogger.java:732)
03-31 00:31:47.459: E/AndroidRuntime(5121):     at com.facebook.AppEventsLogger$2.run(AppEventsLogger.java:605)
03-31 00:31:47.459: E/AndroidRuntime(5121):     at java.util.Timer$TimerImpl.run(Timer.java:284)
03-31 00:31:47.589: D/OpenGLRenderer(5121): Flushing caches (mode 0)
03-31 00:31:50.319: I/Process(5121): Sending signal. PID: 5121 SIG: 9
Was it helpful?

Solution

About the problem with your shared preferences:

It looks like you look up the wrong key here:

long expires = sp.getLong("expires", 0);

you should change it to:

long expires = sp.getLong("access_expires", 0);

because later you do actually save this in the shared preferences:

editor.putLong("access_expires",fb.getAccessExpires());
editor.commit();

Note: The other problem with the NoClassDefFoundError was solved with this related answer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top