The following classes could not be instantiated: - com.facebook.widget.LoginButton

后端 未结 3 1075
感动是毒
感动是毒 2020-12-10 05:44

I have been working on getting the facebook practice apps working and I cannot for the life of me figure out why I cannot reference the LoginButton found in the Facebook SDK

相关标签:
3条回答
  • 2020-12-10 05:50

    //write this first

    FacebookSdk.sdkInitialize(getApplicationContext());

    //then set contentview

        setContentView(R.layout.activity_main);
    
    0 讨论(0)
  • 2020-12-10 05:50

    If you are use android studio then its clear documentation here,

    Use facebook with fragment Tutorial

    User facebook without fragment

    Step 1 :Create java file MyApplication.java inside your package.

    Tutorial

    and copu myApplicatyon.java

    Step 2 :setup 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" />
    

    Step 3 : Init inside activity where you are looking for init Login Button

        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
            }
        });
    
    }
    
    0 讨论(0)
  • 2020-12-10 05:53

    OPTION #1:

    Sometimes, when a new resource is added, Eclipse doesn't compile correctly the new code (maybe a caching issue), causing this error.

    Normally, simply restarting Eclipse is enough to fix the problem.

    OPTION #2:

    Sometimes, rendering custom views causes this exception. The only workaround I know is checking View.isInEditMode every time you try to use the getResources().

    An example could be:

    if (isInEditMode()) {
        //do something else, as getResources() is not valid.
    } else {
        //you can use getResources()
        String mystring = getResources().getString(R.string.mystring);
    }
    
    0 讨论(0)
提交回复
热议问题