Facebook登录按钮错误膨胀。

时间:2016-04-28 22:11:08

标签: java android xml facebook android-fragments

我正在学习片段,我在片段中使用FB Login。但我一直得到一个异常错误膨胀类com.facebook.login.widget.LoginButton

错误: 引起:android.view.InflateException:二进制XML文件行#12:二进制XML文件行#6:二进制XML文件行#6:错误膨胀类com.facebook.login.widget.LoginButton

这是我的Fragment类:

public class MyFragment extends Fragment {
    private LoginButton loginButton;
    private CallbackManager callbackManager;

    public MyFragment() {

    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fb_fragment, container,
        false);

        loginButton = (LoginButton) view.findViewById(R.id.login_button);
        loginButton.setReadPermissions("email");
        FacebookSdk.sdkInitialize(this.getContext());

        // If using in a fragment
        loginButton.setFragment(this);
        // Other app specific specialization
        callbackManager = CallbackManager.Factory.create();

        // Callback registration
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                System.out.println("OnSuccess!");
                // App code
            }

            @Override
            public void onCancel() {
                // App code
            }

            @Override
            public void onError(FacebookException exception) {
                // App code
            }
        });
        return  view;
    }

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

fb_fragment.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.facebook.login.widget.LoginButton
        android:id="@+id/login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="30dp" />

</LinearLayout>

我的MainActivity类

public class MainActivity extends AppCompatActivity {

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

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        MyFragment myFragment = new MyFragment();

        fragmentTransaction.add(R.id.main_activity_fragment, myFragment);
        fragmentTransaction.commit();


    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.beto.fbexample.MainActivity">

    <fragment
        android:id="@+id/main_activity_fragment"
        class="com.example.beto.fbexample.MyFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

对于活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //call this before setContentView
    FacebookSdk.sdkInitialize(getApplicationContext()); 

    setContentView(R.layout.activity_main);

    //...
}

片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    //call this before inflating
    FacebookSdk.sdkInitialize(this.getContext());

    View view = inflater.inflate(R.layout.fb_fragment, container,
    false);

    //...
}

希望有所帮助!