Forcecloses app when launching facebook activity

I have built-in facebook sdk in my android studio project, but the power of the application closes as soon as I launch the application. There is a jsonexception error in the designer, where I used com.facebook.login.widget.LoginButton, but this is only a constructor error.

error log:

> 04-17 19:29:35.996 24734-24734/com.example.bandhan.myapplication1 > E/AndroidRuntime﹕ FATAL EXCEPTION: main > Process: com.example.bandhan.myapplication1, PID: 24734 > java.lang.ExceptionInInitializerError > at java.lang.reflect.Constructor.newInstance(Native Method) > at java.lang.reflect.Constructor.newInstance(Constructor.java:288) > at android.view.LayoutInflater.createView(LayoutInflater.java:614) > at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) > at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) > at android.view.LayoutInflater.inflate(LayoutInflater.java:511) > at android.view.LayoutInflater.inflate(LayoutInflater.java:415) > at android.view.LayoutInflater.inflate(LayoutInflater.java:366) > at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:435) > at android.app.Activity.setContentView(Activity.java:2267) > at com.example.bandhan.myapplication1.Share_Activity.onCreate(Share_Activity.java:15) > at android.app.Activity.performCreate(Activity.java:6289) > at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) > at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) > at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2760) > at android.app.ActivityThread.access$900(ActivityThread.java:177) > at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) > at android.os.Handler.dispatchMessage(Handler.java:102) > at android.os.Looper.loop(Looper.java:145) > at android.app.ActivityThread.main(ActivityThread.java:5944) > 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:1389) > at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184) > 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.newInstance(Native Method) >             at java.lang.reflect.Constructor.newInstance(Constructor.java:288) >             at android.view.LayoutInflater.createView(LayoutInflater.java:614) >             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) >             at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) >             at android.view.LayoutInflater.inflate(LayoutInflater.java:511) >             at android.view.LayoutInflater.inflate(LayoutInflater.java:415) >             at android.view.LayoutInflater.inflate(LayoutInflater.java:366) >             at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:435) >             at android.app.Activity.setContentView(Activity.java:2267) >             at com.example.bandhan.myapplication1.Share_Activity.onCreate(Share_Activity.java:15) >             at android.app.Activity.performCreate(Activity.java:6289) >             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) >             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) >             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2760) >             at android.app.ActivityThread.access$900(ActivityThread.java:177) >             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) >             at android.os.Handler.dispatchMessage(Handler.java:102) >             at android.os.Looper.loop(Looper.java:145) >             at android.app.ActivityThread.main(ActivityThread.java:5944) >             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:1389) >             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184) 

please help me solve the problem. Thanks.

+5
source share
2 answers

If you look at line 99 in the Facebook Validate class, you will see that it throws a FacebookSdkNotInitializedException

You need to call FacebookSdk.sdkInitialize(Context) before loading LoginButton (i.e. your call to setContentView() )

So, in your Activity onCreate method:

 public void onCreate(Bundle savedInstance){ super.onCreate(savedInstance); FacebookSdk.sdkInitialize(getApplicationContext()); setContentView(R.layout.my_layout); // Now you can set the layout with the LoginButton } 

Anytime you intend to use the components of Facebook, you need to make sure that the SDK is initialized.

Also make sure that you have a metadata key for your Facebook application identifier in the manifest, otherwise you will encounter another problem when trying to actually click the LoginButton button.

+10
source

Honestly, this decision is not proof of a fool.

Despite the fact that I called the initialization code before setContentView (), I still get messages about this exception.

What you can do is add an if condition to make sure Facebook has completed the initialization.

if (FacebookSdk.isInitialized ()) // invitation of your application here

0
source

All Articles