1

这是我的主要活动:

package com.FBupdater;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

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

public class MainActivity extends Activity implements OnClickListener {

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fb = new Facebook(APP_ID);

        button = (ImageButton)findViewById(R.id.loginbtn);
        button.setOnClickListener(this);

        updateContentView();
    }

    private void updateContentView() {
        // TODO Auto-generated method stub
        if(fb.isSessionValid()){
            Toast.makeText(MainActivity.this, "done", Toast.LENGTH_LONG).show();
            button.setImageResource(R.drawable.logout);
        }else{
            button.setImageResource(R.drawable.login);
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(fb.isSessionValid()){
            //button close our session - log out of facebook
            try {
                fb.logout(getApplicationContext());
                updateContentView();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else{
            //login to facebook
            fb.authorize(MainActivity.this, new DialogListener() {

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

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

                @Override
                public void onComplete(Bundle values) {
                    // TODO Auto-generated method stub
                    updateContentView(); 
                }

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

                }
            });
        }
    }

}

这是我的 AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.FBupdater"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.FBupdater.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是我的activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_gravity="center"
    android:background="#000000" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="70dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/notloggedinmessage"
            android:textColor="#ffffff"
            android:textSize="20sp" />

        <ImageButton
            android:id="@+id/loginbtn"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@drawable/login"
            android:src="@drawable/login" />

    </LinearLayout>

</LinearLayout>

这是错误日志:

////点击退出按钮后:

11-28 15:22:56.824: D/AndroidRuntime(2123): Shutting down VM
11-28 15:22:56.824: W/dalvikvm(2123): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
11-28 15:22:57.034: E/AndroidRuntime(2123): FATAL EXCEPTION: main
11-28 15:22:57.034: E/AndroidRuntime(2123): android.os.NetworkOnMainThreadException
11-28 15:22:57.034: E/AndroidRuntime(2123):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at com.facebook.android.Util.openUrl(Util.java:215)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at com.facebook.android.Facebook.request(Facebook.java:777)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at com.facebook.android.Facebook.request(Facebook.java:693)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at com.facebook.android.Facebook.logout(Facebook.java:652)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at com.timelystatusupdater.MainActivity.onClick(MainActivity.java:54)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at android.view.View.performClick(View.java:3511)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at android.view.View$PerformClick.run(View.java:14105)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at android.os.Handler.handleCallback(Handler.java:605)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at android.os.Looper.loop(Looper.java:137)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at android.app.ActivityThread.main(ActivityThread.java:4424)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at java.lang.reflect.Method.invokeNative(Native Method)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at java.lang.reflect.Method.invoke(Method.java:511)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-28 15:22:57.034: E/AndroidRuntime(2123):     at dalvik.system.NativeStart.main(Native Method)
11-28 15:22:57.534: D/dalvikvm(2123): GC_CONCURRENT freed 400K, 9% free 5888K/6407K, paused 10ms+34ms
11-28 15:22:57.874: I/dalvikvm(2123): threadid=3: reacting to signal 3
11-28 15:22:57.994: I/dalvikvm(2123): Wrote stack traces to '/data/anr/traces.txt'
11-28 15:23:07.315: I/Process(2123): Sending signal. PID: 2123 SIG: 9

在视频教程中,他说这很简单。他使用了 facebook.logout(Context context) 函数。他的代码运行良好,但我的却不行。

所以,有什么问题,我不明白。提前致谢。

注意:我是 Android 和 Facebook SDK 的新手。事实上,这是我在android中的第一个项目。

我为 min android sdk 8,目标 sdk 16 运行这个项目,当我在 android 2.3.3 (api 10) 模拟器中运行它时,我可以很容易地注销,但是当我在 android 4.0.3 (api 15) 模拟器中运行它时,我可以登录但是当我尝试注销应用程序崩溃并且我收到上面的错误。

我正在关注一个视频教程:mybringback all 11 videos about facebook sdk in android

在视频教程中,他说这很简单。他使用facebook.logout(Context context)函数。他的代码工作正常,但我的不行。

4

4 回答 4

0

在您的 facebook.java 中有一个功能...

public void authorize(Activity activity, String[] permissions, int activityCode, final DialogListener listener) 
    {

        boolean singleSignOnStarted = true;

        mAuthDialogListener = listener;
        Log.d("in authorise", "authorise");
        // Prefer single sign-on, where available.
        **if (activityCode >= 0) 
        {
            Log.d("single sign", "single sign");
            singleSignOnStarted = startSingleSignOn(activity, mAppId, permissions, activityCode);
        }**/////put comment for this code...
        // Otherwise fall back to traditional dialog.
       // else
            startDialogAuth(activity, permissions);

    }
于 2012-11-28T13:51:53.877 回答
0

您使用的 Facebook SDK 版本相当旧(是的,我知道它是最新发布的)。因此,它在内部所做的一些事情是为了在 Froyo (2.2) 上工作,并且不考虑 ICS (4.0) 引入的更改。

ICS 中的一项此类更改是默认情况下启用了应用程序的严格模式。严格模式在运行时对应用程序行为进行大量检查,以确保最佳实践和流畅的用户体验。其中一项检查是应用程序是否正在主线程上执行网络请求。这是一项重要的检查,因为网络请求可能会阻塞很长时间,在此期间 UI(在主线程上呈现)将对用户无响应。

您在 ICS 上看到的错误正是 - 在 Facebooklogout()方法中对主线程上的网络请求的严格检查失败。因此例外NetworkOnMainThread

为避免这种情况,onClickListener您可以使用AsyncTaskwith simple Runnable(使用静态AsyncTask.execute()方法)在另一个线程上启动注销。像这样的东西(在浏览器中编写的代码,所以检查理智):

    // ... cut ...
    if(fb.isSessionValid()){
        //button close our session - log out of facebook
        try {
            AsyncTask.execute(new Runnable() {
                @Override
                public void run() {
                    fb.logout(getApplicationContext());
                    // You can also use MainActivity.this instead of getApplicationContext()
                }
            });
            updateContentView();
        } catch (MalformedURLException e) {
    // ... cut ...

这当然是简单的版本,只是为了解锁你。如果您希望您的 UI 与注销完成时完全同步,您将不得不编写更多代码。

于 2012-11-28T17:46:17.463 回答
0

Facebook API 每天都在变化,这是一个废话,但你可以检查这个项目,我认为它对你来说是完美的

https://github.com/facebook/facebook-android-sdk

于 2012-11-28T19:16:21.677 回答
0

啊哈,我遵循了 AsyncFacebookRunner.java 类并使用了它的注销方法,问题就消失了。

myAsyncFB.logout(Context, RequestListener);
于 2012-11-30T01:14:12.127 回答