Android ClassNotFound and android.view.InflateExce

2020-03-26 04:27发布

问题:

I m trying to integrate the facebook in my app.I am using facebook example app downloaded from facebook as a reference(also generated the appId and all this process on developer.facebook).I am little lazy so I do not want to go through all the code of facebook sdk.In simple words i just add the com_android_facebook library project in my app, and also copy all the classes in facebook example app in my app except Example.java

Now i modify the MyActivity class look like Example.java class, means MyActivity now have all the code of Example class and also my main Activity.

and i change the layout of my app as

    <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout

   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   xmlns:android="http://schemas.android.com/apk/res/android"
  >
  <TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:textSize="22px"
    android:textColor="#ff00ff"
    android:gravity="center"
 >
  </TextView>

<com.android.facebook.LoginButton
 android:id="@+id/login"
 android:src="@drawable/login"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
/>

<Button android:id="@+id/uploadButton"
    android:text="@string/upload"
    android:visibility="invisible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:paddingRight="20dp" 
    android:paddingLeft="20dp"
    android:layout_margin="20dp" 
    />

<Button android:id="@+id/requestButton"
    android:text="@string/request"
    android:visibility="invisible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:paddingRight="20dp" 
    android:paddingLeft="20dp"
    android:layout_margin="20dp" 
    />


<Button android:id="@+id/postButton" 
    android:text="@string/post"
    android:visibility="invisible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:paddingRight="20dp" 
    android:paddingLeft="20dp"
    android:layout_margin="20dp" 
    />

<Button android:id="@+id/deletePostButton" 
    android:text="@string/delete"
    android:visibility="invisible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:paddingRight="20dp" 
    android:paddingLeft="20dp"
    android:layout_margin="20dp" 
    />

</RelativeLayout>

Now when i run my app i am getting there errors

   10-14 00:58:37.786: ERROR/AndroidRuntime(3971): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.myapp/com.android.myapp.MyActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class com.android.facebook.LoginButton
   10-14 00:58:37.786: ERROR/AndroidRuntime(3971): Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class com.android.facebook.LoginButton
   10-14 00:58:37.786: ERROR/AndroidRuntime(3971): Caused by: java.lang.ClassNotFoundException: com.android.facebook.LoginButton in loader dalvik.system.PathClassLoader@44c06850

Any help, suggestions are most welcome...

回答1:

There is no LoginButton in the main source of that SDK.
It's available in the sample code only, so it just fails to load the unavailable class (ClassNotFoundException)...



回答2:

I also faced the same issue. I changed the main.xml from:

<com.facebook.android.LoginButton
    android:id="@+id/login"
    android:src="@drawable/login_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_margin="30dp"
    />

to (Current package is com.facebook.fbtest_simple):

<com.facebook.fbtest_simple.LoginButton 
    android:id="@+id/login"
    android:src="@drawable/login_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_margin="30dp"
    />

As LoginButton is not part of FB standard library.



回答3:

As of version 3.0 of the Facebook SDK, the LoginButton is now available as part of the SDK as com.facebook.widget.LoginButton

See https://developers.facebook.com/docs/reference/android/3.0/LoginButton

Note that Facebook's own upgrade instructions here reference the wrong package, the LoginButton must be declared as com.facebook.widget.LoginButton, not com.facebook.LoginButton



回答4:

Easy one :

   10-14 00:58:37.786: ERROR/AndroidRuntime(3971): Caused by: java.lang.ClassNotFoundException: com.android.facebook.LoginButton in loader dalvik.system.PathClassLoader@44c06850

Looking for in the facebook src , you can go to this path:

AsyncFacebookRunner.java , Facebook.java, FacebookError.java , FbDialog.java, Util.java

..And see that there isn't nothing like LoginButton , then i can think that you are using an old tutorial or something like that.



回答5:

Initializing the Facebook SDK is what worked for me.

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