**MainActivity.java**
facebookbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(fb.isSessionValid())
{
try {
fb.logout(getApplicationContext());
updatebuttonimage();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
fb.authorize(MainActivity.this,new String[] {"email"},new DialogListener() {
@Override
public void onFacebookError(FacebookError e) {
Toast.makeText(MainActivity.this,"FacebookError",Toast.LENGTH_SHORT).show();
}
@Override
public void onError(DialogError e) {
Toast.makeText(MainActivity.this,"Error",Toast.LENGTH_SHORT).show();
}
@Override
public void onComplete(Bundle values) {
Editor editor = sp.edit();
editor.putString("access_token",fb.getAccessToken());
editor.putLong("access_expires",fb.getAccessExpires());
editor.commit();
updatebuttonimage();
}
@Override
public void onCancel() {
Toast.makeText(MainActivity.this,"Cancelled",Toast.LENGTH_SHORT).show();
}
});
}
}
});
private void updatebuttonimage()
{
if(fb.isSessionValid())
{
facebookbutton.setImageResource(R.drawable.fblogout);
pic.setVisibility(ImageView.VISIBLE);
}
else
{
facebookbutton.setImageResource(R.drawable.login);
}
}
**Logcat :**
12-12 10:28:59.843: W/dalvikvm(29773): threadid=1: thread exiting with uncaught exception (group=0x40018578)
12-12 10:28:59.867: E/AndroidRuntime(29773): FATAL EXCEPTION: main
12-12 10:28:59.867: E/AndroidRuntime(29773): java.lang.NullPointerException
12-12 10:28:59.867: E/AndroidRuntime(29773): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.hashCode(HttpConnection.java:314)
12-12 10:28:59.867: E/AndroidRuntime(29773): at java.util.HashMap.get(HashMap.java:302)
12-12 10:28:59.867: E/AndroidRuntime(29773): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:73)
12-12 10:28:59.867: E/AndroidRuntime(29773): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
12-12 10:28:59.867: E/AndroidRuntime(29773): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
12-12 10:28:59.867: E/AndroidRuntime(29773): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1018)
12-12 10:28:59.867: E/AndroidRuntime(29773): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:512)
12-12 10:28:59.867: E/AndroidRuntime(29773): at com.example.facebook_integration.MainActivity.updatebuttonimage(MainActivity.java:140)
12-12 10:28:59.867: E/AndroidRuntime(29773): at com.example.facebook_integration.MainActivity.access$0(MainActivity.java:123)
12-12 10:28:59.867: E/AndroidRuntime(29773): at com.example.facebook_integration.MainActivity$1$1.onComplete(MainActivity.java:106)
12-12 10:28:59.867: E/AndroidRuntime(29773): at com.facebook.android.Facebook.onSessionCallback(Facebook.java:345)
12-12 10:28:59.867: E/AndroidRuntime(29773): at com.facebook.android.Facebook.access$11(Facebook.java:326)
12-12 10:28:59.867: E/AndroidRuntime(29773): at com.facebook.android.Facebook$1.call(Facebook.java:304)
12-12 10:28:59.867: E/AndroidRuntime(29773): at com.facebook.Session$3$1.run(Session.java:1302)
12-12 10:28:59.867: E/AndroidRuntime(29773): at android.os.Handler.handleCallback(Handler.java:587)
12-12 10:28:59.867: E/AndroidRuntime(29773): at android.os.Handler.dispatchMessage(Handler.java:92)
12-12 10:28:59.867: E/AndroidRuntime(29773): at android.os.Looper.loop(Looper.java:130)
12-12 10:28:59.867: E/AndroidRuntime(29773): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-12 10:28:59.867: E/AndroidRuntime(29773): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 10:28:59.867: E/AndroidRuntime(29773): at java.lang.reflect.Method.invoke(Method.java:507)
12-12 10:28:59.867: E/AndroidRuntime(29773): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-12 10:28:59.867: E/AndroidRuntime(29773): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-12 10:28:59.867: E/AndroidRuntime(29773): at dalvik.system.NativeStart.main(Native Method)
我可以连接到Facebook。一旦我登录并返回到我的应用程序,我应该能够看到图像更改,它在updatebuttonimage()方法中实现,但它抛出NullPointerException,并且我已经在onCreate()方法中调用了此方法,但它没有扔掉它然后。我无法弄清楚这个问题。将非常感谢帮助。提前致谢。