Facebook的Android SDK中的applicationID造成NPE [英] Facebook Android SDK applicationId causing NPE
问题描述
我一直在试图让一个Facebook登录(沿着G +登录)在我的应用程序工作。 G +做工精细,现在我已经决定要加入Facebook的整合。
我从Facebook的应用程序ID,并提供了在清单像这样:
<元数据
机器人:名字=com.facebook.skd.ApplicationId
机器人:值=@字符串/ APP_ID/>
下面是字符串资源:
<字符串名称=APP_ID>&0000000000000000 LT; /串>
这是我的登录活动code,对相应的信息审查。我不明白为什么会出现在Facebook的部分 Session.openActiveSession
行的NullPointerException异常,指出争论的applicationID不能为空。的这个链接会为您提供LogCat中的信息,我感到非常新的Android开发,更新与Facebook合作,仅供参考
包com.example.example;进口android.app.Activity;
进口android.app.AlertDialog;
进口android.app.ProgressDialog;
进口android.content.DialogInterface;
进口android.content.Intent;
进口android.content.IntentSender.SendIntentException;
进口android.content.Shared preferences;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.view.WindowManager;进口com.facebook.Request;
进口com.facebook.Response;
进口com.facebook.Session;
进口com.facebook.SessionState;
进口com.facebook.android.Facebook;
进口com.facebook.model.GraphUser;
进口com.facebook.widget.LoginButton;
进口com.google.android.gms.common.ConnectionResult;
进口com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
进口com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
进口com.google.android.gms.common.SignInButton;
进口com.google.android.gms.plus.PlusClient;
进口com.google.android.gms.plus.model.people.Person;
公共类LoginActivity扩展活动实现View.OnClickListener,ConnectionCallbacks,OnConnectionFailedListener {公共静态最后弦乐preFS_NAME =我的prefsFile
公共静态布尔mFirstTimeUsingApp = TRUE;
私有静态最后弦乐TAG = LoginActivity.class.getSimpleName();//谷歌加成员变量
受保护的静态最终诠释REQUEST_ code_RESOLVE_ERR = 9000;
保护PlusClient mPlusClient;
保护ConnectionResult mPlusConnectionResult;
保护ProgressDialog mPlusConnectionProgressDialog;
保护INT SIZE_WIDE = 1;// Facebook的成员变量
私有静态最后弦乐APP_ID =0000000000000000;
私人facebook mFacebook;
私人ProgressDialog mFacebookProgress;
@燮pressWarnings(德precation)
@覆盖
保护无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); 共享preferences mShared preferences = getShared preferences(preFS_NAME,0);
布尔firstTimeUsingapp = mShared preferences.getBoolean(firstTimeUsingApp,真正的);
mPlusClient =新PlusClient.Builder(这,这,这).setVisibleActivities(http://schemas.google.com/AddActivity).build(); 如果(firstTimeUsingapp ==真){ 的setContentView(R.layout.activity_login);
。this.getWindow()setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);/ * * ------------------------------------- /
/ * ----------谷歌加登录---------- * /
/ * * ------------------------------------- / SignInButton btnSignInGooglePlus =(SignInButton)findViewById(R.id.google_plus_sign_in_button);
btnSignInGooglePlus.setSize(SIZE_WIDE); findViewById(R.id.google_plus_sign_in_button).setOnClickListener(本);
//按钮btnSignOutGooglePlus =(按钮)findViewById(R.id.sign_out);
Log.v(TAG,5); Log.v(TAG,6); // mPlusClient =新PlusClient.Builder(这,这,这).setVisibleActivities(http://schemas.google.com/AddActivity).build(); 如果连接故障未解决,显示//进度条。
mPlusConnectionProgressDialog =新ProgressDialog(本);
mPlusConnectionProgressDialog.setMessage(登录...);/ * * ------------------------------------- /
/* - - - - - -Facebook登入 - - - - - - */
/ * * ------------------------------------- / mFacebookProgress =新ProgressDialog(本); LoginButton mFacebookButton =(LoginButton)findViewById(R.id.facebook_sign_in_button); Session.openActiveSession(这一点,真正的,新的Session.StatusCallback(){ @覆盖
公共无效呼叫(会话的会话,SessionState会状态,异常除外){
如果(session.isOpened()){
Request.executeMeRequestAsync(会话,新Request.GraphUserCallback(){ @覆盖
公共无效onCompleted(GraphUser用户,响应响应){
意向意图=新意图(LoginActivity.this,MainActivity.class);
startActivity(意向);
}
});
}
}
});/ * * ------------------------------------- /
/ * ------------微博登录------------ * /
/ * * ------------------------------------- /
//去做/ * * ------------------------------------- /
/ * ----------网站账户登录--------- * /
/ * * ------------------------------------- /
//去做
}
其他{
的setContentView(R.layout.activity_login_pending);
Log.v(TAG,99);
}}@覆盖
保护无效调用onStart(){
Log.v(TAG,在onStart称为);
super.onStart();
Log.v(TAGsuper.onStart称为);
如果(mPlusClient.isConnected()==假){
mPlusClient.connect();
}
Log.v(TAGmPlusClient连接);
}@覆盖
保护无效的onStop(){
super.onStop();
mPlusClient.disconnect();
}
@覆盖
公共无效onConnectionFailed(ConnectionResult结果){
如果(mPlusConnectionProgressDialog.isShowing()){ 如果(result.hasResolution()){
尝试{
result.startResolutionForResult(这一点,REQUEST_ code_RESOLVE_ERR);
}赶上(SendIntentException E){
mPlusClient.connect();
}
}
} mPlusConnectionResult =结果;
}@覆盖
公共无效onConnected(捆绑为arg0){
如果(mPlusConnectionProgressDialog!= NULL){
如果(mPlusConnectionProgressDialog.isShowing()==真){
mPlusConnectionProgressDialog.dismiss();
}
} 人人= mPlusClient.getCurrentPerson(); 字符串userid = person.getId(); 共享preferences共享preferences = getShared preferences(preFS_NAME,0);
共享preferences.Editor编辑=共享preferences.edit(); editor.putString(用户ID,用户ID);
editor.commit(); 意向意图=新意图(LoginActivity.this,MainActivity.class);
startActivity(意向); 完();
}@覆盖
公共无效onDisconnected(){
}@覆盖
保护无效的onActivityResult(INT申请code,INT响应code,意图意图){
如果(要求code == REQUEST_ code_RESOLVE_ERR&放大器;&安培;响应code == RESULT_OK){
mPlusConnectionResult = NULL;
mPlusClient.connect();
}
}@覆盖
公共无效的onClick(查看视图){
如果(view.getId()== R.id.google_plus_sign_in_button&放大器;&放大器;!mPlusClient.isConnected()){
如果(mPlusConnectionResult == NULL){
mPlusConnectionProgressDialog.show();
}其他{
尝试{
mPlusConnectionResult.startResolutionForResult(这一点,REQUEST_ code_RESOLVE_ERR);
}赶上(SendIntentException E){
//尝试重新连接。
mPlusConnectionResult = NULL;
mPlusClient.connect();
mPlusClient.disconnect();
}
}
}否则如果(view.getId()== R.id.facebook_sign_in_button)/ *&放大器;&放大器;不知道到这里TODO * / { }}保护无效errorLogin(){
AlertDialog.Builder建设者=新AlertDialog.Builder(本);
builder.setTitle(R.string.error_login_title);
builder.setMessage(R.string.error_login_message);
builder.setPositiveButton(android.R.string.ok,新DialogInterface.OnClickListener(){ @覆盖
公共无效的onClick(DialogInterface对话,诠释它){
dialog.dismiss(); }
});
AlertDialog对话框= builder.create();
dialog.show();
}}
而不是使用的登录/注销目的的普通按钮,你可以使用Facebook SDK自己的登录/注销的Widget。您可以在下面的链接实现的细节:
<一href=\"http://stackoverflow.com/questions/17683338/post-to-facebook-after-login-fails-android/17684571#17684571\">Post登录后Facebook的失败的Android
编辑:的
初始化后, mFacebookButton
,设置这些权限:
mFacebookButton.setPublishPermissions(Arrays.asList(publish_stream,read_stream));
使用下面的code正确设置权限了。
mFacebookButton.setSessionStatusCallback(新Session.StatusCallback()
{
公共无效呼叫(会话的会话,SessionState会状态,异常除外)
{
// TODO自动生成方法存根
如果(session.isOpened())
{
Log.i(TAG,访问令牌+ session.getAccessToken());
Request.executeMeRequestAsync(会话,新Request.GraphUserCallback(){公共无效onCompleted(GraphUser用户,响应响应)
{
// TODO自动生成方法存根
如果(用户!= NULL)
{
Log.i(TAG,用户ID+ user.getId());
Log.i(TAG,电子邮件+ user.asMap()获得(电子邮件));
}
}});
}
其他
{
//当被点击注销该else条件将被执行。
}
}
});
相反这code的:
Session.openActiveSession(这一点,真正的,新的Session.StatusCallback(){
@覆盖
公共无效呼叫(会话的会话,SessionState会状态,异常除外){
如果(session.isOpened()){
Request.executeMeRequestAsync(会话,新Request.GraphUserCallback(){ @覆盖
公共无效onCompleted(GraphUser用户,响应响应){
意向意图=新意图(LoginActivity.this,MainActivity.class);
startActivity(意向);
}
});
}
}
});
我希望这有助于。
I have been trying to get a Facebook Login (alongside G+ log in) working on my app. G+ works fine, and now I've decided to add Facebook integration.
I have the app ID from facebook, and have provided that in the manifest as so:
<meta-data
android:name="com.facebook.skd.ApplicationId"
android:value="@string/app_id" />
Here is the string resource:
<string name="app_id">000000000000000</string>
And here is my login activity code, with censors on the appropriate information. I do not understand why there is an NullPointerException on the Session.openActiveSession
line in the Facebook section, stating that "Argument 'applicationId' cannot be null. This link will provide you with the LogCat info. I am extremely new to Android development and even more new to working with Facebook, just FYI.
package com.example.example;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.android.Facebook;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.plus.PlusClient;
import com.google.android.gms.plus.model.people.Person;
public class LoginActivity extends Activity implements View.OnClickListener, ConnectionCallbacks, OnConnectionFailedListener{
public static final String PREFS_NAME = "MyPrefsFile";
public static Boolean mFirstTimeUsingApp = true;
private static final String TAG = LoginActivity.class.getSimpleName();
//Google Plus Member Variables
protected static final int REQUEST_CODE_RESOLVE_ERR = 9000;
protected PlusClient mPlusClient;
protected ConnectionResult mPlusConnectionResult;
protected ProgressDialog mPlusConnectionProgressDialog;
protected int SIZE_WIDE = 1;
//Facebook Member Variables
private static final String APP_ID = "000000000000000";
private Facebook mFacebook;
private ProgressDialog mFacebookProgress;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences mSharedPreferences = getSharedPreferences(PREFS_NAME, 0);
boolean firstTimeUsingapp = mSharedPreferences.getBoolean("firstTimeUsingApp", true);
mPlusClient = new PlusClient.Builder(this, this, this).setVisibleActivities("http://schemas.google.com/AddActivity").build();
if (firstTimeUsingapp == true) {
setContentView(R.layout.activity_login);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
/*-------------------------------------*/
/*----------Google Plus Login----------*/
/*-------------------------------------*/
SignInButton btnSignInGooglePlus = (SignInButton) findViewById(R.id.google_plus_sign_in_button);
btnSignInGooglePlus.setSize(SIZE_WIDE);
findViewById(R.id.google_plus_sign_in_button).setOnClickListener(this);
//Button btnSignOutGooglePlus = (Button) findViewById(R.id.sign_out);
Log.v(TAG, "5");
Log.v(TAG, "6");
//mPlusClient = new PlusClient.Builder(this, this, this).setVisibleActivities("http://schemas.google.com/AddActivity").build();
//Progress bar to be displayed if the connection failure is not resolved.
mPlusConnectionProgressDialog = new ProgressDialog(this);
mPlusConnectionProgressDialog.setMessage("Signing in...");
/*-------------------------------------*/
/*-----------Facebook Login------------*/
/*-------------------------------------*/
mFacebookProgress = new ProgressDialog(this);
LoginButton mFacebookButton = (LoginButton) findViewById(R.id.facebook_sign_in_button);
Session.openActiveSession(this, true, new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
}
});
/*-------------------------------------*/
/*------------Twitter Login------------*/
/*-------------------------------------*/
//TODO
/*-------------------------------------*/
/*----------Site Account Login---------*/
/*-------------------------------------*/
//TODO
}
else {
setContentView(R.layout.activity_login_pending);
Log.v(TAG, "99");
}
}
@Override
protected void onStart() {
Log.v(TAG, "onStart called");
super.onStart();
Log.v(TAG, "super.onStart called");
if (mPlusClient.isConnected() == false) {
mPlusClient.connect();
}
Log.v(TAG, "mPlusClient connected");
}
@Override
protected void onStop() {
super.onStop();
mPlusClient.disconnect();
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if(mPlusConnectionProgressDialog.isShowing()) {
if (result.hasResolution()) {
try {
result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
} catch (SendIntentException e){
mPlusClient.connect();
}
}
}
mPlusConnectionResult = result;
}
@Override
public void onConnected(Bundle arg0) {
if (mPlusConnectionProgressDialog != null) {
if (mPlusConnectionProgressDialog.isShowing() == true) {
mPlusConnectionProgressDialog.dismiss();
}
}
Person person = mPlusClient.getCurrentPerson();
String userID = person.getId();
SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("userID", userID);
editor.commit();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
@Override
public void onDisconnected() {
}
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == RESULT_OK) {
mPlusConnectionResult = null;
mPlusClient.connect();
}
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.google_plus_sign_in_button && !mPlusClient.isConnected()) {
if (mPlusConnectionResult == null) {
mPlusConnectionProgressDialog.show();
} else {
try {
mPlusConnectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
} catch (SendIntentException e) {
//Try connecting again.
mPlusConnectionResult = null;
mPlusClient.connect();
mPlusClient.disconnect();
}
}
} else if (view.getId() == R.id.facebook_sign_in_button) /* && not sure what goes here TODO*/ {
}
}
protected void errorLogin() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.error_login_title);
builder.setMessage(R.string.error_login_message);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
Instead of using a normal button for login/logout purposes, you can use Facebook SDK's own Login/Logout Widget. You can find the implementation details in the following link:
Post to facebook after login fails Android
EDIT:
After initializing the mFacebookButton
, set these permissions:
mFacebookButton.setPublishPermissions(Arrays.asList("publish_stream","read_stream"));
Use the code below after setting the permissions correctly.
mFacebookButton.setSessionStatusCallback(new Session.StatusCallback()
{
public void call(Session session, SessionState state,Exception exception)
{
// TODO Auto-generated method stub
if (session.isOpened())
{
Log.i(TAG, "Access Token" + session.getAccessToken());
Request.executeMeRequestAsync(session,new Request.GraphUserCallback() {
public void onCompleted(GraphUser user,Response response)
{
// TODO Auto-generated method stub
if (user != null)
{
Log.i(TAG, "User ID " + user.getId());
Log.i(TAG,"Email "+ user.asMap().get("email"));
}
}});
}
else
{
//This else condition will be executed when logout will be clicked.
}
}
});
Instead of this code:
Session.openActiveSession(this, true, new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
}
});
I hope this helps.
这篇关于Facebook的Android SDK中的applicationID造成NPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!