app forcecloses on starting facebook activity
Asked Answered
E

2

5

I have integrated facebook sdk in my android studio project but app force closes as soon as i run the app. There is jsonexception error in designer where i have used com.facebook.login.widget.LoginButton, but it is only a designer 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 to solve the problem. Thank you.

Erastes answered 17/4, 2015 at 14:8 Comment(0)
O
10

If you look at line 99 in Facebook's Validate class, you'll see it's throwing a FacebookSdkNotInitializedException

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

So, in your Activity's 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're going to use Facebook components, you need to ensure that the SDK is initialized.

Also make sure you have the metadata key for your Facebook Application ID in your manifest, else you're going to run into another issue when you try to actually press the LoginButton.

Optime answered 17/4, 2015 at 15:7 Comment(4)
Yup, you got it right. That worked like a charm. But the problem is i cannot use fb:fetch_user data in xml and uilifecycle in java file.Everything else works perfect. Thank You.Erastes
As long as you make a call to FacebookSdk.sdkInitialize before adding the XML containing your Facebook data then you should be goodOptime
actually fb:fetch_user data in xml and uilifecycle in java file are not getting fetched from the library. Btw i am using latest version of fbsdk. Thank youErastes
This works, but I am so saddened at the lowly documentation Facebook has been providing for us developers who build their platform! Sucks!Athene
C
0

Honestly, that solution is not fool proof.

Even though I called the initialization code before setContentView(), I still get reports of that exception.

What you can do is also add an if condition to be sure that Facebook has finished initialization.

if (FacebookSdk.isInitialized()) // do your app invite here

Challah answered 11/6, 2016 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.