简体   繁体   English

设置Facebook登录按钮的读取权限时,为什么会出现NullPointerException?

[英]Why am I getting a NullPointerException when setting read permissions to Facebook login button?

I'm having a problem with integrating Facebook in my project. 我在将Facebook集成到我的项目中时遇到问题。
I added the dependency to my build.gradle file and the imports and stuff are fine, but when I call the setReadPermissions method of the LoginButton class, I'm getting null pointer. 我将依赖项添加到build.gradle文件中,并且导入和其他操作都很好,但是当我调用LoginButton类的setReadPermissions方法时,我得到的是空指针。
Here's the code I developed: 这是我开发的代码:

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

        // lang selection
        lang = getIntent().getIntExtra("lang", 0);

        AssetManager am = getApplicationContext().getAssets();
        tf = Typeface.createFromAsset(am, "fonts/Biko_Regular.ttf"); 

        lblNombreApp = (TextView) findViewById(R.id.lblTurismoMain);
        lblNombreApp.setTypeface(tf);

        btnLoginOptions = (Button) findViewById(R.id.btnOpenLoginOptions);
        btnLoginOptions.setTypeface(tf);

        loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions("public_profile email");
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                if (AccessToken.getCurrentAccessToken() != null)
                    // facebook
                    Constantes.tipoLogin = 2;

            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException e) {

            }
        });

        // espanol
        if (lang == 1) {
            lblNombreApp.setText(R.string.lblTurismo);
            btnLoginOptions.setText(R.string.lblLoginPopUp);
        }
        // ingles
        else if (lang == 2) {
            lblNombreApp.setText(R.string.lblTurismoEN);
            btnLoginOptions.setText(R.string.lblLoginPopUpEN);
        }

        if (!isNetworkConnected()) {
            if (lang == 1)
                Toast.makeText(getApplicationContext(),
                        R.string.err_no_connection, Toast.LENGTH_LONG).show();
            else if (lang == 2)
                Toast.makeText(getApplicationContext(),
                        R.string.err_no_connectionEN, Toast.LENGTH_LONG).show();

            finish();
        }
    }

I have no idea of what's going on.. Thanks in advance. 我不知道发生了什么。

EDIT 编辑
Here is the layout.. It's a pop up, that's being shown in the main activity. 这是布局。这是一个弹出窗口,显示在主活动中。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/llPopUp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical"
    tools:context=".UI.MainActivity" >

    <TextView
        android:id="@+id/lblLoginPopUp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/lblNoRegistradoPopUp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="7dp"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/btnPopupExistente"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:layout_marginTop="15dp"
        android:onClick="existingUser" />

    <com.facebook.login.widget.LoginButton
        android:id="@+id/login_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:layout_marginTop="15dp" />

    <Button
        android:id="@+id/btnNewPopUp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:layout_marginTop="15dp"
        android:onClick="newUser" />

</LinearLayout>

Thanks in advance. 提前致谢。

If this is the troubling line: 如果这是令人不安的行:

loginButton = (LoginButton) findViewById(R.id.login_button);

make sure the id R.id.login_button exists in your R.layout.activity_main , eg: 确保您的R.layout.activity_main存在ID R.id.login_button ,例如:

<com.facebook.login.widget.LoginButton
    android:id="@+id/login_button"

Also when setting Read Permissions you'd probably want to pass a List<String> ( see docs ) 同样在设置读取权限时,您可能希望传递一个List<String>请参阅docs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM