类强制转换异常不能转换为android.app.Fragment

时间:2013-06-23 09:38:21

标签: android classcastexception android-listfragment

我不确定为什么我会收到此类强制转换异常。我正在使用FragmentActivity中的支持版本ListFragment。

以下是我的活动 package com.thuptencho.transitbus.routes;

import java.util.List;

import android.content.AsyncQueryHandler;
import android.content.BroadcastReceiver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.MergeCursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
public class RouteDetailFragmentActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        L.log();
        mContext = this;
        setContentView(R.layout.activity_route_detail);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        if (savedInstanceState != null) {
            this.mRouteId = savedInstanceState.getInt(C.IntentExtrasKeys.ROUTE_ID);
        }
        else {
            Intent i = getIntent();
            mRouteId = i.getIntExtra(C.IntentExtrasKeys.ROUTE_ID, C.DEFAULT_INVALID_ID);
        }

        String defaultAgency = PreferenceManager.getDefaultSharedPreferences(this).getString(C.Settings.DEFAULT_AGENCY,
                        "none");
        getActionBar().setTitle(defaultAgency.toUpperCase());

        LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
                        new IntentFilter(C.Broadcast.ROUTES_SINGLE_UPDATED_ACTION));
        LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
                        new IntentFilter(C.Broadcast.DIRECTIONS_UPDATED_ACTION));
        LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
                        new IntentFilter(C.Broadcast.D_STOPS_UPDATED_ACTION));
        LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
                        new IntentFilter(C.Broadcast.R_STOPS_UPDATED_ACTION));

        setupView(); //...and so on

下面是我的'activity_route_detail.xml'

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:id="@+id/labelRoute"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_routes"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Spinner
        android:id="@+id/spinnerDirections"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/choose_direction" />

    <Spinner
        android:id="@+id/spinnerStops"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/choose_stop"
        android:spinnerMode="dialog" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Upcoming rides"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ListView
        android:id="@+id/listViewPredictions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <fragment
        android:id="@+id/upcomingRidesFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="com.thuptencho.transitbus.shared.UpcomingRidesFragment"
        android:tag="UpcomingRidesFragment" />

</LinearLayout>

下面是我的ListFragment

package com.thuptencho.transitbus.shared;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ListView;

import com.thuptencho.transitbus.R;
import com.thuptencho.transitbus.routes.PredictionAdapter;
import com.thuptencho.transitbus.utilities.L;

public class UpcomingRidesFragment extends ListFragment {

    private PredictionAdapter adapter;


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        L.log();
        adapter = new PredictionAdapter(getActivity(), R.layout.prediction_listview_item, null);
        super.onActivityCreated(savedInstanceState);
    }
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        L.log();
        super.onListItemClick(l, v, position, id);

    }

}

以下是Logcat错误消息

06-23 05:30:51.121: E/AndroidRuntime(30608): FATAL EXCEPTION: main
06-23 05:30:51.121: E/AndroidRuntime(30608): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thuptencho.transitbus/com.thuptencho.transitbus.routes.RouteDetailFragmentActivity}: android.view.InflateException: Binary XML file line #40: Error inflating class fragment
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.os.Looper.loop(Looper.java:137)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.app.ActivityThread.main(ActivityThread.java:5041)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at java.lang.reflect.Method.invokeNative(Native Method)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at java.lang.reflect.Method.invoke(Method.java:511)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at dalvik.system.NativeStart.main(Native Method)
06-23 05:30:51.121: E/AndroidRuntime(30608): Caused by: android.view.InflateException: Binary XML file line #40: Error inflating class fragment
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.app.Activity.setContentView(Activity.java:1881)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at com.thuptencho.transitbus.routes.RouteDetailFragmentActivity.onCreate(RouteDetailFragmentActivity.java:48)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.app.Activity.performCreate(Activity.java:5104)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-23 05:30:51.121: E/AndroidRuntime(30608):    ... 11 more
06-23 05:30:51.121: E/AndroidRuntime(30608): Caused by: java.lang.ClassCastException: com.thuptencho.transitbus.shared.UpcomingRidesFragment cannot be cast to android.app.Fragment
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.app.Fragment.instantiate(Fragment.java:585)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.app.Fragment.instantiate(Fragment.java:560)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.app.Activity.onCreateView(Activity.java:4709)
06-23 05:30:51.121: E/AndroidRuntime(30608):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)

2 个答案:

答案 0 :(得分:3)

您的RouteDetailFragmentActivity是否延长了android.support.v4.app.FragmentActivity

使用支持库时必须小心。您必须记住将您常用的Activity,Fragment等类替换为支持库中的类。在继续之前,您应该阅读this页面。

答案 1 :(得分:0)

com.thuptencho.transitbus.shared.UpcomingRidesFragment它正在扩展android.support.v4.app.ListFragment,但有些你正在尝试将其转换为android.app.Fragment ..它应该是android.support.v4.app。 Frgament。