0

我有一个应用程序,它使用自定义标题功能。我正在尝试将其迁移到 Fragments api。我得到的结果代码是这样的:

public class MainActivity extends CategoriesListActivity {

    protected static final String TAG = "MainActivity";

    /** Called when the activity is first created. */
    public void onActivityCreated(Bundle savedInstanceState) {
        getActivity().requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        super.onActivityCreated(savedInstanceState);
        Thread.setDefaultUncaughtExceptionHandler(new MecomUncaughtExceptionHandler(getActivity()));

        getActivity().setContentView(R.layout.main);
        getActivity().getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.main_title);
    }
}

在哪里CategoriesListActivity延伸ListFragmentmain_title.xml这是:

<?xml version="1.0" encoding="utf-8"?>
<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    class="com.mecom.berlingske.fragments.HeaderFragment"
    android:id="@+id/header"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

HeaderFragment是:

public class HeaderFragment extends Fragment implements OnClickListener {    
    private static final String TAG = "Header";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.main_title_fragment, container, false);
    }
}

并且main_title_fragment.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_height="wrap_content" android:layout_width="fill_parent">
    <ImageView
        android:id="@+id/berlingske_logo"
        android:src="@drawable/main_title_bg"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:onClick="onClick"/>
    <View
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent"
        android:visibility="invisible"/>
    <Button
        android:id="@+id/buttonTitleSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableTop="@drawable/ic_menu_search"
        android:layout_gravity="fill_vertical"
        style="@style/BerlingskeMainTitleButton"
        android:onClick="onClick"/>
     <Button
        android:id="@+id/buttnTitleFeedback"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableTop="@drawable/ic_menu_light"
        android:layout_gravity="fill_vertical"
        style="@style/BerlingskeMainTitleButton"
        android:onClick="onClick"/>
</LinearLayout>

当我运行它时,它在Activity膨胀后崩溃:

E/AndroidRuntime(  545): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mecom.berlingske/com.mecom.berlingske.MainActivity}: java.lang.ClassCastException: com.mecom.berlingske.MainActivity
E/AndroidRuntime(  545):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
E/AndroidRuntime(  545):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
E/AndroidRuntime(  545):    at android.app.ActivityThread.access$2200(ActivityThread.java:119)
E/AndroidRuntime(  545):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
E/AndroidRuntime(  545):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  545):    at android.os.Looper.loop(Looper.java:123)
E    /AndroidRuntime(  545):    at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime(  545):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  545):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  545):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime(  545):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime(  545):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  545): Caused by: java.lang.ClassCastException: com.mecom.berlingske.MainActivity
E/AndroidRuntime(  545):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
E/AndroidRuntime(  545):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
E/AndroidRuntime(  545):    ... 11 more

我在这里看不到任何演员阵容问题。有人试图将片段设置为自定义标题吗?

4

1 回答 1

2

查看堆栈跟踪,您似乎将MainActivity其视为实际ActivityFragment. 看起来您要么不完全理解,要么Fragments您的应用程序设计存在缺陷。

于 2011-04-18T07:53:36.860 回答