【问题标题】:Android ClassNotFound and android.view.InflateException:Android ClassNotFound 和 android.view.InflateException:
【发布时间】:2011-12-07 06:36:46
【问题描述】:

我正在尝试将 facebook 集成到我的应用程序中。我正在使用从 facebook 下载的 facebook 示例应用程序作为参考(还生成了 appIddeveloper.facebook 上的所有这些过程)。我有点懒所以我不想浏览facebook sdk的所有代码。简单来说,我只是在我的应用程序中添加com_android_facebook库项目,并在我的应用程序中复制facebook示例应用程序中的所有类,除了Example.java

现在我修改 MyActivity 类看起来像 Example.java 类,这意味着 MyActivity 现在拥有 Example 类的所有代码以及我的主要 Activity。

我将应用程序的布局更改为

    <?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>

现在当我运行我的应用程序时,我遇到了错误

   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

欢迎任何帮助,建议......

【问题讨论】:

标签: android facebook class facebook-android-sdk


【解决方案1】:

我也遇到了同样的问题。我将 main.xml 更改为:

<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"
    />

到(当前包是 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"
    />

因为 LoginButton 不是 FB 标准库的一部分。

【讨论】:

    【解决方案2】:

    从 Facebook SDK 3.0 版开始,LoginButton 现在作为 SDK 的一部分提供 com.facebook.widget.LoginButton

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

    注意Facebook自己的升级说明here引用了错误的包,LoginButton必须声明为com.facebook.widget.LoginButton,而不是com.facebook.LoginButton

    【讨论】:

    • 从 4.2.0 开始,此按钮似乎已移至 com.facebook.login.widget.LoginButton
    【解决方案3】:

    该 SDK 的主要源代码中没有 LoginButton
    它仅在示例代码中可用,因此无法加载不可用的类(ClassNotFoundException)...

    【讨论】:

    • 但我也复制了 LoginButton 类。
    • 您确定它位于包com.android.facebook 中。我最后的猜测......
    【解决方案4】:

    简单的一个:

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

    在facebook src中寻找,可以到这个path

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

    ..并且看到没有像 LoginButton 这样的东西,那么我可以认为您使用的是旧教程或类似的东西。

    【讨论】:

      【解决方案5】:

      初始化 Facebook SDK 对我有用。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多