我是
Android新手.我试图将FB登录与我的Android应用程序集成.我已经按照developers.facebook.com上的所有说明进行了此集成.但是,当我尝试运行我的应用程序时,它会在启动时崩溃.
请注意,我的AndroidManifest.xml中已经有Facebook App ID,元数据标签,android活动标签.
以下是布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background"> <RelativeLayout android:id="@+id/container" android:layout_width="match_parent" android:paddingBottom="20dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="20dp" android:layout_height="match_parent" android:background="#85000000"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/topSocialLoginLayout" android:paddingLeft="40dp" android:paddingRight="40dp" android:layout_marginTop="10dp" android:layout_marginBottom="5dp"> <com.facebook.login.widget.LoginButton android:id="@+id/btnFacebook" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:layout_weight="5"/> <com.google.android.gms.common.SignInButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:id="@+id/btnGoogle" android:layout_weight="5" /> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingRight="20dp" android:paddingLeft="20dp" android:id="@+id/relLayoutMainLogin" android:layout_below="@id/topSocialLoginLayout"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:inputType="textEmailAddress" android:layout_alignParentTop="true" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:hint="@string/email" android:textColor="#fd9a22" android:textCursorDrawable="@drawable/cursor_color" android:textColorHint="#ffffff" android:id="@+id/txtUser" android:background="@drawable/edit_text" android:drawableLeft="@drawable/dr_email" android:drawablePadding="10dp" android:paddingLeft="-3dp" android:singleLine="true" /> <EditText android:layout_width="match_parent" android:inputType="textPassword" android:layout_height="wrap_content" android:layout_below="@id/txtUser" android:id="@+id/txtPwd" android:hint="@string/pwd" android:textColor="#fd9a22" android:textCursorDrawable="@drawable/cursor_color" android:textColorHint="#ffffff" android:layout_marginTop="10dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:background="@drawable/edit_text" android:drawableLeft="@drawable/dr_pwd" android:drawablePadding="10dp"/> <Button android:layout_width="match_parent" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="10dp" android:layout_marginBottom="5dp" android:layout_below="@id/txtPwd" android:layout_height="wrap_content" android:text="@string/sign_in" android:id="@+id/btnSignIn" android:background="@drawable/ainovatheme_btn_default_holo_light" android:onClick="login"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/btnSignIn" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#fd9a22" android:background="#85000000" android:id="@+id/txtLoginErr" android:layout_marginBottom="5dp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@id/txtLoginErr" android:text="@string/forgot_pwd" android:textColor="#ffffff" android:id="@+id/btnForgotPwd" android:onClick="forgotPassword" style="?android:attr/borderlessButtonStyle"/> <ImageView android:layout_width="fill_parent" android:id="@+id/hrLine" android:layout_height="2dp" android:layout_below="@id/btnForgotPwd" android:background="#d4dce9" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/hrLine" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_centerHorizontal="true" android:text="@string/create_account" android:textColor="#fd9a22" android:id="@+id/btnCreateAccountActivity" android:onClick="createAccount" style="?android:attr/borderlessButtonStyle" /> </RelativeLayout> </RelativeLayout> </RelativeLayout>
以下是我的LoginActivity的代码段:
private CallbackManager mCallbackManager; private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { GraphRequest request = GraphRequest.newMeRequest(Accesstoken.getCurrentAccesstoken(),new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted(JSONObject object,GraphResponse response) { try { String email = object.getString("email"); Log.d("Output" + "User email ",email); String password = object.toString(); Log.d("Output" + "User psw",password); } catch (JSONException e) { // Todo Auto-generated catch block e.printstacktrace(); } } }); request.executeAsync(); } @Override public void onCancel() { } @Override public void onError(FacebookException e) { } }; // ####### Facebook Sign In Coding - End ####### @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); //check for internet connection if (!isConnectedToInternet()) { //dialog if internet is not connetd createNetErrorDialog(); } FacebookSdk.sdkInitialize(this); // ####### Facebook Sign In Coding mCallbackManager = CallbackManager.Factory.create(); // ####### Facebook Sign In Coding LoginButton FBloginButton = (LoginButton) findViewById(R.id.btnFacebook); // ####### Facebook Sign In Coding FBloginButton.setReadPermissions(Arrays.asList("public_profile,email,user_birthday")); // ####### Facebook Sign In Coding FBloginButton.registerCallback(mCallbackManager,mCallback); // ####### Facebook Sign In Coding } @Override public void onActivityResult(int requestCode,int resultCode,Intent data) { super.onActivityResult(requestCode,resultCode,data); mCallbackManager.onActivityResult(requestCode,data); }
以下是我得到的例外情况:
java.lang.ExceptionInInitializerError at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:417) at android.view.LayoutInflater.createView(LayoutInflater.java:593) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:693) at android.view.LayoutInflater.rInflate(LayoutInflater.java:752) at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) at android.view.LayoutInflater.inflate(LayoutInflater.java:495) at android.view.LayoutInflater.inflate(LayoutInflater.java:397) at android.view.LayoutInflater.inflate(LayoutInflater.java:353) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:360) at android.app.Activity.setContentView(Activity.java:1932) at ainova.jaesa.android.main.LoginActivity.onCreate(LoginActivity.java:97) at android.app.Activity.performCreate(Activity.java:5326) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2225) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) at android.app.ActivityThread.access$700(ActivityThread.java:158) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5365) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) at dalvik.system.NativeStart.main(Native Method) Caused by: null at com.facebook.internal.Validate.sdkInitialized(Validate.java:99) at com.facebook.FacebookSdk.getCallbackRequestCodeOffset(FacebookSdk.java:735) at com.facebook.internal.CallbackManagerImpl$RequestCodeOffset.toRequestCode(CallbackManagerImpl.java:109) at com.facebook.login.widget.LoginButton.<clinit>(LoginButton.java:58) at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:417) at android.view.LayoutInflater.createView(LayoutInflater.java:593) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:693) at android.view.LayoutInflater.rInflate(LayoutInflater.java:752) at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) at android.view.LayoutInflater.inflate(LayoutInflater.java:495) at android.view.LayoutInflater.inflate(LayoutInflater.java:397) at android.view.LayoutInflater.inflate(LayoutInflater.java:353) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:360) at android.app.Activity.setContentView(Activity.java:1932) at ainova.jaesa.android.main.LoginActivity.onCreate(LoginActivity.java:97) at android.app.Activity.performCreate(Activity.java:5326) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2225) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) at android.app.ActivityThread.access$700(ActivityThread.java:158) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5365) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) at dalvik.system.NativeStart.main(Native Method)
解决方法
1.你的第一个错误:
Caused by: null at com.facebook.internal.Validate.sdkInitialized(Validate.java:99)
它正在抛出FacebookSdknotinitializedException
你需要在setContentView()之前调用FacebookSdk.sdkInitialize(Context)
在Activity的onCreate方法中,将其更改为:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // here FacebookSdk.sdkInitialize(this); // ####### Facebook Sign In Coding setContentView(R.layout.login); //check for internet connection if (!isConnectedToInternet()) { //dialog if internet is not connetd createNetErrorDialog(); }
Update
2.第二个错误:NullPointerException:
你得到一个NullPointerException,因为你在调用setContentView()之前调用了findViewById(),所以当你调用loginButton.registerCallback()时,loginButton为null.
只需将调用setContentView()移到顶部:(完整的oncreate方法应该是这样的):
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FacebookSdk.sdkInitialize(this); // ####### Facebook Sign In Coding setContentView(R.layout.login); //check for internet connection if (!isConnectedToInternet()) { //dialog if internet is not connetd createNetErrorDialog(); } mCallbackManager = CallbackManager.Factory.create(); // ####### Facebook Sign In Coding LoginButton FBloginButton = (LoginButton) findViewById(R.id.btnFacebook); // ####### Facebook Sign In Coding FBloginButton.setReadPermissions(Arrays.asList("public_profile,mCallback); // ####### Facebook Sign In Coding }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。