誰かが私の次のコードでnullpointer例外を解決するのを手伝ってくれませんか?
私は次のものを持っていますFoodStarLoginActivity
。次の行はNullPointerをスローしていますが、理由がわかりません:facebook.logout(FoodStarLoginActivity.this);
誰かが私を助けてくれますか?getApplicationcontext()
代わりに試してみましFoodStarLoginActivity
たが、うまくいきませんでした。
I am getting following exception :
java.lang.NullPointerException
at android.context.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
at android.webkit.CookieSyncManager.createInstance(CookiesSyncManager.java:96)
at com.facebook.android.Util.clearCookies(Util.java:241)
at com.facebook.android.Facebook.logout(Facebook.java:617)
at ten.mob.socialapp.FoodStarLoginActivity.logout(FoodStarLoginActivity.java:131)
package ten.mob.socialapp;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import ten.mob.socialapp.FacebookConnector;
import ten.mob.socialapp.SessionEvents;
import ten.mob.socialapp.FacebookConnector.LogoutRequestListener;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
public class FoodStarLoginActivity extends Activity{
private static final String faceBookAppId = "*******************";
private static final String faceBookPermission = "publish_stream";
static Facebook facebook = new Facebook(faceBookAppId);
private SharedPreferences mPrefs;
FacebookConnector facebookConnector ;
AsyncFacebookRunner asyncRunner;
private Handler mHandler = new Handler();
Context context;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if(access_token != null){
facebook.setAccessToken(access_token);
}
if(expires != 0){
facebook.setAccessExpires(expires);
}
if(!facebook.isSessionValid()){
facebook.authorize(this, new DialogListener(){
@Override
public void onComplete(Bundle values){
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
}
@Override
public void onFacebookError(FacebookError error){}
@Override
public void onError(DialogError e){}
@Override
public void onCancel(){}
});
}
if(facebook.isSessionValid()){
Intent intent = new Intent(this,SearchMyActivity.class);
startActivity(intent);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
public void logout(){
try {
facebook.logout(FoodStarLoginActivity.this);
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
asyncRunner = new AsyncFacebookRunner(facebook);
System.out.println(asyncRunner);
asyncRunner.logout(FoodStarLoginActivity.this, new LogoutRequestListener() {
public void onComplete(String response, Object state) {}
public void onFileNotFoundException(FileNotFoundException e,Object state) {}
public void onFacebookError(FacebookError e, Object state) {}
});
System.out.println(asyncRunner);
System.out.println(facebook.isSessionValid());
}
private class LogoutRequestListener implements RequestListener {
public void onComplete(String response, Object state) {
mHandler.post(new Runnable() {
public void run() {
SessionEvents.onLogoutFinish();
}
});
}
public void onIOException(IOException e, Object state) {}
public void onFileNotFoundException(FileNotFoundException e,Object state) {}
public void onMalformedURLException(MalformedURLException e,Object state) {}
public void onFacebookError(FacebookError e, Object state) {}
}
}
public class SearchMyActivity(){
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menu_create:
Intent intent = new Intent(this, CreateRestaurantActivity.class);
startActivity(intent);
break;
case R.id.menu_foodstars:
intent = new Intent(this, RestaurantList.class);
startActivity(intent);
break;
case R.id.menu_logout:
FoodStarLoginActivity logoutFoodStar = new FoodStarLoginActivity();
logoutFoodStar.logout();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
}