Java facebook登录空指针异常android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29454586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
facebook login null pointer exception android
提问by user2752910
I searched a lot through stackoverflow but I couldn't find an answer that resolves my issue. Hence I am posting this.
我通过 stackoverflow 搜索了很多,但找不到解决我问题的答案。因此我发布了这个。
I have a sample Android App in which I'm trying to use facebook Login. Here are the steps that I followed:
我有一个示例 Android 应用程序,我试图在其中使用 Facebook 登录。以下是我遵循的步骤:
Environment: Android studio Android SDK version : 22 Facebook SDK : 4.0
环境:Android studio Android SDK 版本:22 Facebook SDK:4.0
Followed the steps to install FB APK on the emulator, generated the Developer hash key and updated it in app-> settings. My android manifest has these code
按照步骤在模拟器上安装 FB APK,生成开发者哈希密钥并在 app-> 设置中更新它。我的 android manifest 有这些代码
<uses-permission android:name="android.permission.INTERNET"/>
<activity android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="@android:style/Theme.Translucent.NoTitleBar" android:label="@string/app_name" />
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
In my activity_main.xml layout file, I have this piece of code
在我的 activity_main.xml 布局文件中,我有这段代码
<com.facebook.login.widget.LoginButton
android:id="@+id/fblogin_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp" />
And finally in my main Activity I have this
最后在我的主要活动中我有这个
public class MainActivity extends Activity {
LoginButton loginButton;
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.fblogin_button);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Toast.makeText(getApplicationContext(),"Fb Login Success",Toast.LENGTH_LONG);
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(),"Fb on cancel",Toast.LENGTH_LONG);
}
@Override
public void onError(FacebookException e) {
Toast.makeText(getApplicationContext(),"Fb Login Error",Toast.LENGTH_LONG);
}
});
setContentView(R.layout.activity_main);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
Log.d("Inside Fb login","on Activiry result");
callbackManager.onActivityResult(requestCode, resultCode, data);
}}
I am getting a null pointer exception as shown below
我收到一个空指针异常,如下所示
java.lang.RuntimeException: Unable to start activity componentInfo{PROJECT.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.login.widget.LoginButton.registerCallback(com.facebook.CallbackManager, com.facebook.FacebookCallback)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access0(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
What am I missing here ?
我在这里错过了什么?
回答by Daniel Nugent
Looks like you're getting a NullPointerException
because you're calling findViewById()
before you call setContentView()
, so loginButton
is null when you call loginButton.registerCallback()
.
看起来你得到 aNullPointerException
因为你在你打电话findViewById()
之前打电话setContentView()
,所以loginButton
当你打电话时是空的loginButton.registerCallback()
。
Just move the call to setContentView()
to the top:
只需将调用移至setContentView()
顶部:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main); //Moved up here to resolve NPE
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.fblogin_button);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Toast.makeText(getApplicationContext(),"Fb Login Success",Toast.LENGTH_LONG);
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(),"Fb on cancel",Toast.LENGTH_LONG);
}
@Override
public void onError(FacebookException e) {
Toast.makeText(getApplicationContext(),"Fb Login Error",Toast.LENGTH_LONG);
}
});
}
回答by anj
The crash seems to be related to the applicationId we are setting in the AndoridMenifest.xml
file. As i figured out that when facebook sdk is tying to create the LoginFramment
then ApplicationId is comming as null
.Here is onCreate method of com.facebook.login.LoginFragment
class
崩溃似乎与我们在AndoridMenifest.xml
文件中设置的 applicationId 有关。正如我发现当 facebook sdk 正在绑定创建LoginFramment
然后 ApplicationId 来作为null
.Here 是com.facebook.login.LoginFragment
类的onCreate 方法
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null) {
this.loginClient = (LoginClient)savedInstanceState.getParcelable("loginClient");
this.loginClient.setFragment(this);
} else {
this.loginClient = new LoginClient(this);
}
this.loginClient.setOnCompletedListener(new OnCompletedListener() {
public void onCompleted(Result outcome) {
LoginFragment.this.onLoginClientCompleted(outcome);
}
});
FragmentActivity activity = this.getActivity();
if(activity != null) {
this.initializeCallingPackage(activity);
if(activity.getIntent() != null) {
//the applicationId in this.request object is null
this.request = (Request)activity.getIntent().getParcelableExtra("request");
}
}
}
As pointed out by the varoius post the applicationId is coming null because the string resource app_id
is not being parsed correctly by android.
https://github.com/jamesward/AndroidStringBug
正如 varoius 帖子所指出的,applicationId 将变为 null,因为app_id
android 未正确解析字符串资源。
https://github.com/jamesward/AndroidStringBug
I solved this probelm by setting the applicationId manually after initializing the facebook sdk as given below in onCreate
method.
我通过在初始化 facebook sdk 后手动设置 applicationId 解决了这个问题,如下面的onCreate
方法所示。
FacebookSdk.sdkInitialize(getApplicationContext());
FacebookSdk.setApplicationId(getResources().getString(R.string.app_id));
mFacebookCallbackManager = CallbackManager.Factory.create();
回答by batmaci
as anj indicated problem is usually with the ApplicationId was not read. in my case, Problem was with the namespace. make sure that your namespace of the class is the root namespace. So my app is an xamarin android app and i was using namespace as below but my Android App's namespace is myApp.Droid, this way ApplicaitonId is not read from strings.xml changing to myApp.Droid fixed my problem. I hope that it helps anyone else having similar issue
正如 anj 所指出的,问题通常是没有读取 ApplicationId。就我而言,问题出在命名空间上。确保您的类的命名空间是根命名空间。所以我的应用程序是一个 xamarin android 应用程序,我使用如下命名空间,但我的 Android 应用程序的命名空间是 myApp.Droid,这样 ApplicaitonId 不是从 strings.xml 读取更改为 myApp.Droid 解决了我的问题。我希望它可以帮助其他有类似问题的人
[assembly: MetaData("com.facebook.sdk.ApplicationId", Value = "@string/app_id")]
namespace myApp.Droid.Renderer