我无法在我的方法中访问sharedPreferences接口的getPreferences方法。我在我的方法logintofacebook中使用了getpreferences方法但是它给了我一个PRIVATE_MODE的错误我该如何解决这个问题。
这是我编辑过的代码。
public class FacebookUtils {
private static final DialogListener DEFAULT_AUTH_ACTIVITY_CODE = null;
private static String APP_ID = "783826255024540";
public Facebook facebook = new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
Button btnFbLogin;
Button btnFbGetProfile;
Context mcontext;
public FacebookUtils() {
// TODO Auto-generated constructor stub
}
public void authorize(Activity activity, String[] permissions,
final DialogListener listener) {
authorize(activity, permissions, DEFAULT_AUTH_ACTIVITY_CODE);
}
public FacebookUtils(Context context) {
}
@SuppressWarnings("unused")
public void loginToFacebook() {
SharedPreferences sharedPreferences=mcontext.getSharedPreferences("", Context.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);
btnFbLogin.setVisibility(View.INVISIBLE);
btnFbGetProfile.setVisibility(View.VISIBLE);
Log.d("FB Sessions", "" + facebook.isSessionValid());
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize((Activity) mcontext,
new String[] { "email", "publish_stream" },
new DialogListener() {
@Override
public void onCancel() {
}
@Override
public void onComplete(Bundle values) {
SharedPreferences sharedPreferences=mcontext.getSharedPreferences("", Context.MODE_PRIVATE);
Editor editor=sharedPreferences.edit();
editor.putString("access_token",facebook.getAccessToken());
editor.putLong("access_expires",facebook.getAccessExpires());
editor.commit();
btnFbLogin.setVisibility(View.INVISIBLE);
btnFbGetProfile.setVisibility(View.VISIBLE);
}
@Override
public void onError(DialogError error) {
// Function to handle error
}
@Override
public void onFacebookError(FacebookError fberror) {
}
});
}
}
public void getprofileinformation()
{
mAsyncRunner.request("me", new RequestListener() {
@Override
public void onMalformedURLException(MalformedURLException e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onFileNotFoundException(FileNotFoundException e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onComplete(String response, Object state) {
// TODO Auto-generated method stub
Log.d("Profile", response);
String json = response;
try {
JSONObject profile = new JSONObject(json);
final String name = profile.getString("name");
final String email = profile.getString("email");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(mcontext, "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
protected void runOnUiThread(Runnable runnable) {
// TODO Auto-generated method stub
}
}
这是logcat错误。
12-02 13:31:36.648: E/AndroidRuntime(2524): FATAL EXCEPTION: main
12-02 13:31:36.648: E/AndroidRuntime(2524): Process: com.facebook.androidhive, PID: 2524
12-02 13:31:36.648: E/AndroidRuntime(2524): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
12-02 13:31:36.648: E/AndroidRuntime(2524): at com.facebook.demo.FacebookUtils.loginToFacebook(FacebookUtils.java:56)
12-02 13:31:36.648: E/AndroidRuntime(2524): at com.facebook.demo.AndroidFacebookConnectActivity$1.onClick(AndroidFacebookConnectActivity.java:51)
12-02 13:31:36.648: E/AndroidRuntime(2524): at android.view.View.performClick(View.java:4756)
12-02 13:31:36.648: E/AndroidRuntime(2524): at android.view.View$PerformClick.run(View.java:19749)
12-02 13:31:36.648: E/AndroidRuntime(2524): at android.os.Handler.handleCallback(Handler.java:739)
12-02 13:31:36.648: E/AndroidRuntime(2524): at android.os.Handler.dispatchMessage(Handler.java:95)
12-02 13:31:36.648: E/AndroidRuntime(2524): at android.os.Looper.loop(Looper.java:135)
12-02 13:31:36.648: E/AndroidRuntime(2524): at android.app.ActivityThread.main(ActivityThread.java:5221)
12-02 13:31:36.648: E/AndroidRuntime(2524): at java.lang.reflect.Method.invoke(Native Method)
12-02 13:31:36.648: E/AndroidRuntime(2524): at java.lang.reflect.Method.invoke(Method.java:372)
12-02 13:31:36.648: E/AndroidRuntime(2524): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-02 13:31:36.648: E/AndroidRuntime(2524): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)`
这是我第二次打电话给这个班级的活动。
public class AndroidFacebookConnectActivity extends Activity {
private static String APP_ID = "783826255024540";
private Facebook facebook = new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
Button btnFbLogin;
Button btnFbGetProfile;
@SuppressWarnings("unused")
private FacebookUtils utils;
Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnFbLogin = (Button) findViewById(R.id.btn_fblogin);
btnFbGetProfile = (Button) findViewById(R.id.btn_get_profile);
mAsyncRunner = new AsyncFacebookRunner(facebook);
utils = new FacebookUtils();
btnFbLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Image Button", "button Clicked");
utils.loginToFacebook(mContext);
}
});
btnFbGetProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
utils.getprofileinformation();
}
});
}
错误与此活动有关? 我在这一行得到了null异常。 String access_token = mPrefs.getString(“access_token”,null);
答案 0 :(得分:4)
您尚未初始化变量mPrefs
。因此,当您尝试mPrefs.getString("access_token", null);
null
;
截至目前,您只使用声明
声明了它private SharedPreferences mPrefs;
但是为了访问它,您还必须将其初始化为
mPrefs = getPreferences(MODE_PRIVATE);
在尝试使用它之前。
答案 1 :(得分:2)
这对你来说很容易: -
放置值
SharedPreferences sharedPreferences=mContext.getSharedPreferences("", Context.MODE_PRIVATE);
Editor editor=sharedPreferences.edit();
editor.putString("access_token",facebook.getAccessToken());
editor.putLong("access_expires",facebook.getAccessExpires());
editor.commit();
获取值
SharedPreferences sharedPreferences=mContext.getSharedPreferences("", Context.MODE_PRIVATE);
String access_token = sharedPreferences.getString("access_token", null);
long expires = sharedPreferences.getLong("access_expires", 0);
检查构造函数一次。可能是mContext为null。试试这个: -
public FacebookUtils(Context context) {
mContext=context;
}
尝试将上下文传递给loginToFacebook(): -
public void loginToFacebook(Context mContext) {
}
答案 2 :(得分:0)
万一有人面临这个问题:
public class listops {
Context mcontext;
SharedPreferences prefs;
public listops(Context context)
{
mcontext=context;
prefs= mcontext.getSharedPreferences("prefs", Context.MODE_PRIVATE);
}
...
}
这将解决问题。