使用 Fragments 自定义标题

发布于 2024-11-01 18:14:56 字数 4739 浏览 1 评论 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< 后崩溃/code> 膨胀了:

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

我在这里没有看到任何转换问题。有人尝试将片段设置为自定义标题吗?

I've got an app, that uses custom title feature. I'm trying to migrate it to fragments api. Resulting code, I've got, is like this:

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);
    }
}

Where CategoriesListActivity extends ListFragment. main_title.xml is this:

<?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 is:

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);
    }
}

and main_title_fragment.xml is:

<?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>

When I run this, it crashes after this Activity is inflated:

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

I don't see any cast problems here. Anyone tried to set a fragment to be a custom title?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2024-11-08 18:14:56

查看堆栈跟踪,您似乎将 MainActivity 视为实际的 Activity,但事实并非如此,它是一个 Fragment。您似乎没有完全理解 Fragments 或者您的应用程序的设计存在缺陷。

Looking at the stack trace it seems like you are treating MainActivity as an actual Activity, which it is not, it is a Fragment. It seems like you either don't fully understand Fragments or something about the design of your application is flawed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文