10

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. Below is the error that I am encountering when I look at the layout that defines the 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)
Coova
  • 1,818
  • 5
  • 36
  • 63
  • Anyone have any idea as to why this is happening? I cannot seem to get any response. – Coova Jun 05 '13 at 23:57
  • Eclipse is trying to render the preview of the element but can't load the resource at 0x7F070004, apparently a color resource. Check the generated R.java to figure out which element that is. Either it isn't in your project, or missing from one of the libraries. ... I think.... I have trouble with this for resources that work fine when running the app on a device or emulator. I'm still researching the problem... – Jon Adams Jun 06 '13 at 03:06
  • 1
    I have the same problem, I noticed it happens cause of using google play services, I don't have a solution yet – user1325188 Jun 09 '13 at 07:10
  • Having same problem any solution please update with u r answer –  Aug 18 '13 at 21:37

3 Answers3

32

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);
}
Alejandro Colorado
  • 6,034
  • 2
  • 28
  • 39
  • 1
    this is very helpful comment which helps a number of other issues. – gjnave Dec 12 '13 at 23:56
  • Option #1 didnt help in Android Studio v0.8.14. Following tip message but I'm unsure how to implement this tip - thoughts?: Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE – natur3 Nov 24 '14 at 18:32
  • Found an answer to my own issue. I'm using navigation drawer and its calling the onCreateView() from the View. I simply added the following line View rootView = blah blah; rootView.isInEditMode(); This now allows me to use the graphical IDE for designing. – natur3 Nov 24 '14 at 18:36
  • Option #1 helped. ty – codemaniac Jun 03 '15 at 12:19
2

//write this first

FacebookSdk.sdkInitialize(getApplicationContext());

//then set contentview

    setContentView(R.layout.activity_main);
G Mehdi Balti
  • 154
  • 2
  • 12
1

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
        }
    });

}
Ronak Amlani
  • 654
  • 7
  • 15