Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9242671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:37:42+00:00 2026-06-18T08:37:42+00:00

When i click the login button it logs me in facebook (asking for permission

  • 0

When i click the login button it logs me in facebook (asking for permission the first time) and everything is fine. But when i click logout after that i receive the “unfortunately myapp has stopped” error and the app closes. I looked up for solution to that problem but couldnt find any that works for me. Here is my code:

import java.io.IOException;
import java.net.MalformedURLException;

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

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainMenu extends Activity {

ImageView btnLogin;
Facebook fb;
private SharedPreferences shared_pref;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);
    btnLogin = (ImageView) findViewById(R.id.buttonFb);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    String APP_ID = getString(R.string.APP_ID);
    fb = new Facebook(APP_ID);

    //tryToGetDataFromPreviousLogin();

    createButtons();
}

private void tryToGetDataFromPreviousLogin() {
    shared_pref = getPreferences(MODE_PRIVATE);
    String access_token = shared_pref.getString("access_token", null);
    long expires = shared_pref.getLong("access_expires", 0);

    if(access_token != null) {
        fb.setAccessToken(access_token);
    }

    if(expires != 0) {
        fb.setAccessExpires(expires);
    }
}

private void createButtons() {
    createBtnExit();
    createBtnStart();
    createBtnFbLogin();
    createBtnSettings();        
}

private void createBtnExit() {
    Button btnExit = (Button) findViewById(R.id.buttonExit);
    btnExit.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            popQuitDialog();
        }
    });
}

private void createBtnStart() {
    Button btnStart = (Button) findViewById(R.id.buttonStart);
    btnStart.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(MainMenu.this, MainActivity.class);
            MainMenu.this.startActivity(intent);
        }
    });
}

private void createBtnFbLogin() {
    btnLogin.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if(fb.isSessionValid()) {
                try {
                    fb.logout(MainMenu.this);
                    updateFbButtonImage();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            else {
                fb.authorize(MainMenu.this, new DialogListener() {

                    @Override
                    public void onFacebookError(FacebookError e) {
                        Toast.makeText(MainMenu.this, "onFacebookError", Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onError(DialogError e) {
                        Toast.makeText(MainMenu.this, "onError", Toast.LENGTH_SHORT).show();                            
                    }

                    @Override
                    public void onComplete(Bundle values) {
                        /*
                        Editor editor = shared_pref.edit();
                        editor.putString("access_token", fb.getAccessToken());
                        editor.putLong("access_expires", fb.getAccessExpires());
                        editor.commit(); */
                        updateFbButtonImage();                          
                    }

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

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

private void updateFbButtonImage() {
    if(fb.isSessionValid()) {
        btnLogin.setImageResource(R.drawable.logout_button);
    }
    else {
        btnLogin.setImageResource(R.drawable.login_button);
    }
}

private void createBtnSettings() {
    Button btnSettings = (Button) findViewById(R.id.buttonStatistics);
    btnSettings.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(MainMenu.this, Statistics.class);
            MainMenu.this.startActivity(intent);
        }
    });
}

public void popQuitDialog() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainMenu.this);

    alertDialog.setTitle("Do you really want to quit?");
    alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });

    alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    alertDialog.show();
}

}

this is the stacktrace (running it on my android 4.0.4):

 01-25 19:09:06.028: D/dalvikvm(23201): GC_FOR_ALLOC freed 50K, 7% free 12768K/13663K, paused 23ms
01-25 19:09:06.088: I/dalvikvm-heap(23201): Grow heap (frag case) to 21.362MB for 9216016-byte allocation
01-25 19:09:06.118: D/dalvikvm(23201): GC_FOR_ALLOC freed <1K, 5% free 21767K/22691K, paused 15ms
01-25 19:09:06.168: D/dalvikvm(23201): GC_CONCURRENT freed 1K, 5% free 21766K/22691K, paused 2ms+2ms
01-25 19:09:06.248: D/dalvikvm(23201): GC_FOR_ALLOC freed <1K, 5% free 21766K/22691K, paused 12ms
01-25 19:09:06.368: I/dalvikvm-heap(23201): Grow heap (frag case) to 36.985MB for 16384016-byte allocation
01-25 19:09:06.398: D/dalvikvm(23201): GC_FOR_ALLOC freed 0K, 3% free 37766K/38755K, paused 19ms
01-25 19:09:06.448: D/dalvikvm(23201): GC_CONCURRENT freed 0K, 3% free 37767K/38755K, paused 2ms+3ms
01-25 19:09:06.558: D/TextLayoutCache(23201): Using debug level: 0 - Debug Enabled: 0
01-25 19:09:06.568: D/libEGL(23201): loaded /system/lib/egl/libGLES_android.so
01-25 19:09:06.568: D/libEGL(23201): loaded /system/lib/egl/libEGL_adreno200.so
01-25 19:09:06.578: D/libEGL(23201): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
01-25 19:09:06.588: D/libEGL(23201): loaded /system/lib/egl/libGLESv2_adreno200.so
01-25 19:09:06.598: I/Adreno200-EGLSUB(23201): <ConfigWindowMatch:2078>: Format RGBA_8888.
01-25 19:09:06.618: D/memalloc(23201): /dev/pmem: Mapped buffer base:0x5c6c6000 size:21016576 offset:17248256 fd:64
01-25 19:09:06.618: E/(23201): Can't open file for reading
01-25 19:09:06.618: E/(23201): Can't open file for reading
01-25 19:09:06.628: D/OpenGLRenderer(23201): Enabling debug mode 0
01-25 19:09:06.818: D/memalloc(23201): /dev/pmem: Mapped buffer base:0x5ee47000 size:11022336 offset:7254016 fd:67
01-25 19:09:09.048: D/memalloc(23201): /dev/pmem: Mapped buffer base:0x5f9d2000 size:7254016 offset:3485696 fd:70
01-25 19:09:09.208: D/OpenGLRenderer(23201): Flushing caches (mode 1)
01-25 19:09:10.038: D/OpenGLRenderer(23201): Flushing caches (mode 0)
01-25 19:09:10.038: D/memalloc(23201): /dev/pmem: Unmapping buffer base:0x5c6c6000 size:21016576 offset:17248256
01-25 19:09:10.038: D/memalloc(23201): /dev/pmem: Unmapping buffer base:0x5ee47000 size:11022336 offset:7254016
01-25 19:09:10.038: D/memalloc(23201): /dev/pmem: Unmapping buffer base:0x5f9d2000 size:7254016 offset:3485696
01-25 19:09:16.588: I/Adreno200-EGLSUB(23201): <ConfigWindowMatch:2078>: Format RGBA_8888.
01-25 19:09:16.598: D/memalloc(23201): /dev/pmem: Mapped buffer base:0x5c6c6000 size:7254016 offset:3485696 fd:64
01-25 19:09:16.738: D/memalloc(23201): /dev/pmem: Mapped buffer base:0x5ceb1000 size:11563008 offset:7794688 fd:67
01-25 19:09:18.498: D/memalloc(23201): /dev/pmem: Mapped buffer base:0x600ce000 size:21016576 offset:17248256 fd:70
01-25 19:09:18.578: D/Facebook-Util(23201): GET URL: https://api.facebook.com/restserver.php?access_token=BAAHxNZCKUYeABAOumBne8GijZA3qZAUimZBQVsGu19qjSfRXIWBSy5t5WpsT3SV4GmqSFFZAdOWpmu7SJrH51V245ZBHh1RMMQybkpP2OH7GRDGMNAgc0Fudaztq07ZCVRMbcHb14nfYAZDZD&method=auth.expireSession&format=json
01-25 19:09:18.578: D/AndroidRuntime(23201): Shutting down VM
01-25 19:09:18.578: W/dalvikvm(23201): threadid=1: thread exiting with uncaught exception (group=0x40a9f210)
01-25 19:09:18.608: E/AndroidRuntime(23201): FATAL EXCEPTION: main
01-25 19:09:18.608: E/AndroidRuntime(23201): android.os.NetworkOnMainThreadException
01-25 19:09:18.608: E/AndroidRuntime(23201):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1108)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at libcore.io.IoBridge.connectErrno(IoBridge.java:133)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at libcore.io.IoBridge.connect(IoBridge.java:118)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at java.net.Socket.connect(Socket.java:849)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:77)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:117)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at com.facebook.android.Util.openUrl(Util.java:219)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at com.facebook.android.Facebook.requestImpl(Facebook.java:806)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at com.facebook.android.Facebook.request(Facebook.java:709)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at com.facebook.android.Facebook.logoutImpl(Facebook.java:651)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at com.facebook.android.Facebook.logout(Facebook.java:644)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at taxi.route.MainMenu$3.onClick(MainMenu.java:93)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at android.view.View.performClick(View.java:3574)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at android.view.View$PerformClick.run(View.java:14293)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at android.os.Handler.handleCallback(Handler.java:605)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at android.os.Looper.loop(Looper.java:137)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at android.app.ActivityThread.main(ActivityThread.java:4441)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at java.lang.reflect.Method.invokeNative(Native Method)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at java.lang.reflect.Method.invoke(Method.java:511)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
01-25 19:09:18.608: E/AndroidRuntime(23201):    at dalvik.system.NativeStart.main(Native Method)
01-25 19:09:22.068: I/Process(23201): Sending signal. PID: 23201 SIG: 9

Also as you can see the commented code is a code that should try to log in automatically the user but it doesnt work as intended and i can’t find a solution for it aswell. I dont know if i have to make new question about it so advises are appreciated.


This is what i did added this in MainMenu.java

private class PostTask extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... params) {
        try {
            fb.logout(MainMenu.this);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}

And replace commented with

//fb.logout(MainMenu.this);
  new PostTask().doInBackground();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:37:44+00:00Added an answer on June 18, 2026 at 8:37 am

    You are getting a NetworkOnMainThreadException as indicated by your stack trace. This happens on Android versions 3.0+ since they have a new StrictMode which does not allow network operations to be done on the main (native Thread). Consider using an AsyncTask to do your network stuff.

    Here is an example of an AsyncTask which. It consists of two basic parts – doInBackground() which does the network stuff on a separate Thread, then passes its result over to onPostExecute(). In this second method, you make the necessary changes to the UI. Like the example notes, if you want to do something before your doInBackground() method executes, you use onPreExecute().

    Also an interesting read: Keeping Your App Responsive. It also provides an AsyncTask example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to design a webpage login that when click on the login button
I try to provide a login with the facebook-php-sdk. But, when I click on
I'm a newbie of jQuery. Now I'm trying to intercept click login button that
This is my Facebook connect Code: <script> jQuery(document).ready(function($){ $(.fb-login-button).live('click', function() { console.log('click'); fbEnsureInit(function() {
I am using Facebook Connect . When I click on the Log In button
For example if you go to facebook and press double click on the login
I am showing a modal popup when someone click login button and then they
I have some Java Swing login panel. When user click login button, extensive DB
Possible Duplicate: How to let Facebook Login button redirect to a particular URL I
I have created a login form. It works fine, but it goes back to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.