android.app.Fragment$InstantiationException: 尝试实例化一个 class 不是片段
android.app.Fragment$InstantiationException: Trying to instantiate a class that is not a Fragment
我的activity代码:
public class TrackActivity extends FragmentActivity implements
OnMarkerClickListener, OnMarkerDragListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.track_layout);
}
track_layout代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/rl_track_main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.jd.activity.TrackActivity" />
<LinearLayout
android:id="@+id/ll_btm_btns"
android:layout_width="match_parent"
android:layout_height="@dimen/height_track_bottom_btns"
android:layout_alignParentBottom="true"
android:weightSum="2" >
<Button
android:id="@+id/btn1_track"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/round_corner_track_bottom_btn"
android:text="@string/track"
android:textColor="@color/white"
android:textStyle="bold" />
<Button
android:id="@+id/btn2_trace"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/round_corner_track_bottom_btn"
android:text="@string/trace"
android:textColor="@color/white"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/drawer_rl_track"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@drawable/drawer_title_shape"
android:gravity="center"
android:textSize="20sp" />
<ListView
android:id="@+id/list_view_drawer_track"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:choiceMode="singleChoice"
android:divider="@android:color/darker_gray"
android:dividerHeight="0.1dp"
android:textColor="@android:color/white" />
<Button
android:id="@+id/btn_fleet_trk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="@drawable/round_rectangle"
android:gravity="center"
android:text="@string/fleet_track"
android:textColor="@color/white"
android:textSize="15sp" />
</LinearLayout>
我遇到错误
02-24 17:50:47.437: E/AndroidRuntime(7637): FATAL EXCEPTION: main
02-24 17:50:47.437: E/AndroidRuntime(7637): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jd.activity/com.jd.activity.TrackActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.ActivityThread.access0(ActivityThread.java:158)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.os.Looper.loop(Looper.java:176)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.ActivityThread.main(ActivityThread.java:5365)
02-24 17:50:47.437: E/AndroidRuntime(7637): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 17:50:47.437: E/AndroidRuntime(7637): at java.lang.reflect.Method.invoke(Method.java:511)
02-24 17:50:47.437: E/AndroidRuntime(7637): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-24 17:50:47.437: E/AndroidRuntime(7637): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-24 17:50:47.437: E/AndroidRuntime(7637): at dalvik.system.NativeStart.main(Native Method)
02-24 17:50:47.437: E/AndroidRuntime(7637): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
02-24 17:50:47.437: E/AndroidRuntime(7637): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:360)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.Activity.setContentView(Activity.java:1932)
02-24 17:50:47.437: E/AndroidRuntime(7637): at com.jd.activity.TrackActivity.onCreate(TrackActivity.java:57)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.Activity.performCreate(Activity.java:5326)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
02-24 17:50:47.437: E/AndroidRuntime(7637): ... 11 more
02-24 17:50:47.437: E/AndroidRuntime(7637): Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.jd.activity.TrackActivity that is not a Fragment
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.Fragment.instantiate(Fragment.java:584)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.Fragment.instantiate(Fragment.java:560)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.Activity.onCreateView(Activity.java:4908)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
02-24 17:50:47.437: E/AndroidRuntime(7637): ... 22 more
02-24 17:50:47.437: E/AndroidRuntime(7637): Caused by: java.lang.ClassCastException
02-24 17:50:47.437: E/AndroidRuntime(7637): ... 26 more
这个错误很容易解释。膨胀试图膨胀你的 TrackActivity - 这是一个 FragmentActivity,而不是一个片段。您需要扩展片段。
这里
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.jd.activity.TrackActivity" />// exception occurs here
出现异常是因为您使用了 FragmentAcitivity
而不是 Fragment
。
TrackActivity
扩展 FragmentActivity
,因此不是 Fragment
。
去掉
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.jd.activity.TrackActivity" />
来自您的布局
我不小心解决了这个问题:)
用 class="com.google.android.gms.maps.MapFragment"
替换 class="com.jd.activity.TrackActivity"
解决了问题...不知道为什么 :(
我的activity代码:
public class TrackActivity extends FragmentActivity implements
OnMarkerClickListener, OnMarkerDragListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.track_layout);
}
track_layout代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/rl_track_main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.jd.activity.TrackActivity" />
<LinearLayout
android:id="@+id/ll_btm_btns"
android:layout_width="match_parent"
android:layout_height="@dimen/height_track_bottom_btns"
android:layout_alignParentBottom="true"
android:weightSum="2" >
<Button
android:id="@+id/btn1_track"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/round_corner_track_bottom_btn"
android:text="@string/track"
android:textColor="@color/white"
android:textStyle="bold" />
<Button
android:id="@+id/btn2_trace"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/round_corner_track_bottom_btn"
android:text="@string/trace"
android:textColor="@color/white"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/drawer_rl_track"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@drawable/drawer_title_shape"
android:gravity="center"
android:textSize="20sp" />
<ListView
android:id="@+id/list_view_drawer_track"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:choiceMode="singleChoice"
android:divider="@android:color/darker_gray"
android:dividerHeight="0.1dp"
android:textColor="@android:color/white" />
<Button
android:id="@+id/btn_fleet_trk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="@drawable/round_rectangle"
android:gravity="center"
android:text="@string/fleet_track"
android:textColor="@color/white"
android:textSize="15sp" />
</LinearLayout>
我遇到错误
02-24 17:50:47.437: E/AndroidRuntime(7637): FATAL EXCEPTION: main
02-24 17:50:47.437: E/AndroidRuntime(7637): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jd.activity/com.jd.activity.TrackActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.ActivityThread.access0(ActivityThread.java:158)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.os.Looper.loop(Looper.java:176)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.ActivityThread.main(ActivityThread.java:5365)
02-24 17:50:47.437: E/AndroidRuntime(7637): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 17:50:47.437: E/AndroidRuntime(7637): at java.lang.reflect.Method.invoke(Method.java:511)
02-24 17:50:47.437: E/AndroidRuntime(7637): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-24 17:50:47.437: E/AndroidRuntime(7637): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-24 17:50:47.437: E/AndroidRuntime(7637): at dalvik.system.NativeStart.main(Native Method)
02-24 17:50:47.437: E/AndroidRuntime(7637): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
02-24 17:50:47.437: E/AndroidRuntime(7637): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:360)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.Activity.setContentView(Activity.java:1932)
02-24 17:50:47.437: E/AndroidRuntime(7637): at com.jd.activity.TrackActivity.onCreate(TrackActivity.java:57)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.Activity.performCreate(Activity.java:5326)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
02-24 17:50:47.437: E/AndroidRuntime(7637): ... 11 more
02-24 17:50:47.437: E/AndroidRuntime(7637): Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.jd.activity.TrackActivity that is not a Fragment
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.Fragment.instantiate(Fragment.java:584)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.Fragment.instantiate(Fragment.java:560)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.app.Activity.onCreateView(Activity.java:4908)
02-24 17:50:47.437: E/AndroidRuntime(7637): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
02-24 17:50:47.437: E/AndroidRuntime(7637): ... 22 more
02-24 17:50:47.437: E/AndroidRuntime(7637): Caused by: java.lang.ClassCastException
02-24 17:50:47.437: E/AndroidRuntime(7637): ... 26 more
这个错误很容易解释。膨胀试图膨胀你的 TrackActivity - 这是一个 FragmentActivity,而不是一个片段。您需要扩展片段。
这里
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.jd.activity.TrackActivity" />// exception occurs here
出现异常是因为您使用了 FragmentAcitivity
而不是 Fragment
。
TrackActivity
扩展 FragmentActivity
,因此不是 Fragment
。
去掉
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.jd.activity.TrackActivity" />
来自您的布局
我不小心解决了这个问题:)
用 class="com.google.android.gms.maps.MapFragment"
替换 class="com.jd.activity.TrackActivity"
解决了问题...不知道为什么 :(