Android:以下类无法实例化com.facebooklogin.widget.Loginbutton

时间:2015-09-03 10:12:37

标签: java android xml facebook android-studio

当我尝试查看包含该文件的xml文件的视图(settings_activity.xml)时,我在android studio中遇到错误(问题的标题):

<fragment
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/fragment"
            android:name="fr.djey.trollstory.FacebookConnectActivityFragment"
            tools:layout="@layout/fragment_facebook_connect"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="0dp"
            android:layout_marginBottom="25dp"/>

这个片段是这个xml(fragment_facebook_connect.xml):

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/text_details"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_above="@+id/login_button"/>

    <com.facebook.login.widget.LoginButton
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:id="@+id/login_button"
        />

</LinearLayout>

我不明白,因为在我的java文件中,我初始化了facebook sdk(在包含facebook片段代码的java中,以及包含活动代码的java)。

包含片段的部分java(FacebookConnectActivityFragment):

    package fr.djey.***;

import android.content.Intent;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.ProfileTracker;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

/**
 * A placeholder fragment containing a simple view.
 */
public class FacebookConnectActivityFragment extends Fragment {

    private TextView mTextDetails;

    private AccessTokenTracker mTokenTracker;
    private ProfileTracker mProfileTracker;

    private CallbackManager mCallbackManager;
    private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            Profile profile = Profile.getCurrentProfile();
            displayWelcomeMessage(profile);
        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException e) {

        }
    };

    public FacebookConnectActivityFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
        super.onCreate(savedInstanceState);
        mCallbackManager = CallbackManager.Factory.create();
        AccessTokenTracker tracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {


            }
        };
        ProfileTracker profileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
                displayWelcomeMessage(newProfile);
            }
        };
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_facebook_connect, container, false);
    }

    private void displayWelcomeMessage(Profile profile) {
        if (profile != null) {
            mTextDetails.setText("Salut " + profile.getFirstName());
        }
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LoginButton LoginButton = (LoginButton) view.findViewById(R.id.login_button);
        LoginButton.setReadPermissions("user_friends");
        LoginButton.setFragment(this);
        LoginButton.registerCallback(mCallbackManager, mCallback);

        mTextDetails = (TextView) view.findViewById(R.id.text_details);
    }

    @Override
    public void onResume() {
        super.onResume();
        Profile profile = Profile.getCurrentProfile();
        displayWelcomeMessage(profile);
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mCallbackManager.onActivityResult(requestCode,resultCode,data);
    }
}

包含整个活动的一部分java(SettingsActivity):

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_settings);

在build.gradle中,我把它放在:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.facebook.android:facebook-android-sdk:4.0.1'
    compile 'com.google.android.gms:play-services:7.8.0'
    compile 'com.android.support:multidex:1.0.1'
}

问题是当我启动应用程序和facebook按钮时应用程序正常工作,但我无法在xml中看到它。

我给你我的机器人清单(不是所有的活动):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="fr.djey.***">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />

    <application
        android:name="android.support.multidex.MultiDexApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id" />

        <activity
            android:name=".SettingsActivity"
            android:label="@string/title_activity_settings" >
        </activity>
        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />
    </application>

</manifest>

所以我有另一个问题,也许问题可能是双元数据?我想在我的应用中添加google和facebook连接,所以我不知道如何处理元数据。

1 个答案:

答案 0 :(得分:0)

Make sure you have following in your manifest.xml

**facebook appID**

<string name="app_id">YOUR_FACEBOOK_APP_ID</string>\

**Meta Data** 

<meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id" />

**permission**

<uses-permission android:name="android.permission.INTERNET" />

**Facebook Login Activity**

activity
          android:name="com.facebook.LoginActivity"
          android:theme="@style/AppTheme" >
      </activity>