android.app.Fragment $ InstantiationException:尝试实例化一个不是Fragment的类

时间:2015-02-24 12:27:00

标签: android google-maps exception android-fragments instantiation

我的活动代码:

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.access$700(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

4 个答案:

答案 0 :(得分:4)

TrackActivity延长FragmentActivity,而ergo不是Fragment

摆脱

 <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.jd.activity.TrackActivity" />

来自您的布局

答案 1 :(得分:1)

下面

<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

答案 2 :(得分:0)

错误非常自我解释。膨胀试图让你的TrackActivity膨胀 - 这是一个FragmentActivity,而不是Fragment。你需要扩展Fragment。

答案 3 :(得分:0)

我意外地解决了这个问题:)

class="com.jd.activity.TrackActivity"替换class="com.google.android.gms.maps.MapFragment"解决了这个问题...不知道原因:(