这是我的主要活动:
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)
函数。他的代码工作正常,但我的不行。