我一直在努力让facebook练习应用程序正常工作,我不能为我的生活弄清楚为什么我不能引用Facebook SDK中的LoginButton。下面是我在查看定义LoginButton的布局时遇到的错误。
<com.facebook.widget.LoginButton
android:id="@+id/authButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
/>
The following classes could not be instantiated:
- com.facebook.widget.LoginButton (Open Class, Show Error Log)
See the Error Log (Window > Show View) for more details.
Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse
android.content.res.Resources$NotFoundException: Could not resolve resource value: 0x7F070004.
at android.content.res.BridgeResources.throwException(BridgeResources.java:693)
at android.content.res.BridgeResources.getColor(BridgeResources.java:185)
at com.facebook.widget.LoginButton.<init>(LoginButton.java:211)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0( at sun.reflect.NativeConstructorAccessorImpl.newInstance( at sun.reflect.DelegatingConstructorAccessorImpl.newInstance( at java.lang.reflect.Constructor.newInstance( at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:422)
at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:179)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:135)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:746)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:372)
答案 0 :(得分:32)
选项#1:
有时,当添加新资源时,Eclipse无法正确编译新代码(可能是缓存问题),从而导致此错误。
通常,只需重新启动Eclipse即可解决问题。
选项#2:
有时,渲染自定义视图会导致此异常。我知道的唯一解决方法是每次尝试使用getResources()
时都要检查View.isInEditMode。
一个例子可能是:
if (isInEditMode()) {
//do something else, as getResources() is not valid.
} else {
//you can use getResources()
String mystring = getResources().getString(R.string.mystring);
}
答案 1 :(得分:2)
//先写这个
FacebookSdk.sdkInitialize(getApplicationContext());
//然后设置contentview
setContentView(R.layout.activity_main);
答案 2 :(得分:1)
如果你使用的是android studio,那么这里有明确的文档,
将facebook与片段一起使用 Tutorial
没有片段的用户脸书
步骤1:在包中创建java文件MyApplication.java。
和copu myApplicatyon.java
第2步:设置androidmenufest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Myapptheme"
android:name=".MyApplication"
>
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id" />
<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" />
步骤3:在您正在寻找init登录按钮的内部活动
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
callbackManager = CallbackManager.Factory.create();
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_auth_login);
//Init Elements
etEmail = (EditText) findViewById(R.id.etEmail);
etPassword = (EditText) findViewById(R.id.etPassword);
validator = new Validator(this);
validator.setValidationListener(this);
serverConnection = new ServerConnection();
//Faceboo login init
loginButton = (LoginButton) findViewById(R.id.btnFbLogin);
loginButton.setReadPermissions(Arrays.asList("public_profile","email","user_photos"));
// Other app specific specialization
// Callback registration
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
Profile profile = Profile.getCurrentProfile();
Log.v("profile.getName:",profile.getName());
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
}