我已将android sdk集成到我的android工作室项目中,但app force在我运行应用程序后立即关闭。 设计器中有jsonexception错误,我使用过com.facebook.login.widget.LoginButton,但它只是一个设计器错误。
错误日志:
> 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)
请帮我解决问题。 谢谢。
答案 0 :(得分:10)
如果您查看Facebook Validate
课程中的第99行,您会发现它正在投掷FacebookSdkNotInitializedException
您需要在加载FacebookSdk.sdkInitialize(Context)
之前致电LoginButton
(即致电setContentView()
)
所以,在Activity
的{{1}}方法中
onCreate
任何时候您都要使用Facebook组件,您需要确保SDK已初始化。
另外,请确保在清单中有Facebook应用程序ID的元数据键,否则当您尝试实际按下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
}
时,您将遇到另一个问题。
答案 1 :(得分:0)
老实说,这个解决方案不是万无一失的。
即使我在setContentView()之前调用了初始化代码,我仍然会收到该异常的报告。
您可以做的是添加if条件以确保Facebook已完成初始化。
if(FacebookSdk.isInitialized())//在此处邀请您的应用