【问题标题】:Getting Class Cast Exception when extends to Fragment in android..?扩展到android中的Fragment时获取Class Cast Exception ..?
【发布时间】:2014-07-10 05:30:40
【问题描述】:

我正在开发一个将 MainActivity 扩展到 Fragment 的应用程序。

   public class MainActivity extends Fragment implements LocationListener{...}

当我在 android 4.0 及更高版本中执行代码时,它运行良好。当我在 android 2.3 中执行时,它会出现类转换异常。

LogCat 中的错误

   E/AndroidRuntime(11123): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.test.fragment/com.test.fragment.MainActivity}: java.lang.ClassCastException: com.test.fragment.MainActivity

   E/AndroidRuntime(11123): Caused by: java.lang.ClassCastException: com.test.fragment.MainAcvitiy

在 android 2.3 版本设备中执行代码时出现以下错误,但 cmg 到 4.0 及更高版本运行良好。

添加了支持片段和操作栏的 v4 和 v7 库,但仍然面临同样的问题。

【问题讨论】:

  • 看起来您正在将片段投射到 Activity。您的帖子中缺少该部分
  • 您是否必须在应用程序中设置支持库才能在 Android 2.3 或更低版本中运行 Fragments?

标签: android android-fragments fragment android-fragmentactivity


【解决方案1】:

请查看您的包导入声明

v4 支持库中关于 Fragment 的正确包导入语句为:

import android.support.v4.app.Fragment

但不是

import android.app.Fragment

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多