为什么我的应用程序在Facebook注销时崩溃了?使用facebook sdk

时间:2013-01-25 17:25:31

标签: android facebook login sdk

当我点击登录按钮时,它会在facebook中记录我(第一次请求许可),一切都很好。但是,当我点击注销后,我收到“不幸的是myapp已经停止”错误,应用程序关闭。我找到了解决这个问题的办法,但找不到对我有用的东西。这是我的代码:

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();
}

}

这是堆栈跟踪(在我的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

另外正如您所看到的,注释代码是一个代码,应该尝试自动登录用户,但它不能按预期工作,我也找不到解决方案。我不知道我是否必须提出新的问题,所以建议表示赞赏。


这就是我在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;
    }

}

并替换为

评论
//fb.logout(MainMenu.this);
  new PostTask().doInBackground();

1 个答案:

答案 0 :(得分:1)

您正在获得堆栈跟踪指示的NetworkOnMainThreadException。这种情况发生在Android版本3.0+上,因为它们有一个新的StrictMode,它不允许在main(本机线程)上完成网络操作。考虑使用AsyncTask来处理您的网络。

Here是AsyncTask的一个例子。它由两个基本部分组成 - doInBackground()单独的线程上执行网络内容,然后将其结果传递给onPostExecute()。在第二种方法中,您对UI进行必要的更改。与示例说明一样,如果您想在执行 之前执行某些操作,则使用doInBackground()

还有一个有趣的读物:Keeping Your App Responsive。它还提供了一个AsyncTask示例。