在片段中创建工具栏

时间:2016-02-22 08:03:23

标签: android android-fragments android-toolbar

我有使用片段的滑动视图的应用程序。我想要的是为每个片段创建单独的工具栏。香港专业教育学院搜索互联网并使用推荐的解决方案,但当我想使用setSupportActionBar()时,当我尝试将MainActivity强制转换为AppCompact Activity时,我的应用程序崩溃了。继承人片段类:

package sk.cassomedia.studentassitent.view;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import sk.cassomedia.studentassitent.R;

public class Contacts extends Fragment {

    private Toolbar toolbar;

    public Contacts() {
    // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_contacts, container, false);
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        toolbar = (Toolbar) view.findViewById(R.id.contacts_toolbar);
        activity.setSupportActionBar(toolbar);
        return view;
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getActivity().getMenuInflater().inflate(R.menu.calendar_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    public static Fragment newInstance() {
        Contacts contacts = new Contacts();

        return contacts;
    }
}

继承片段布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="sk.cassomedia.studentassitent.view.Contacts">

<include
    android:id="@+id/contacts_toolbar"
    layout="@layout/contacts_toolbar"></include>

<ListView
    android:layout_width="wrap_content"
    android:layout_height="836dp"
    android:id="@+id/listView"
    android:layout_gravity="center_horizontal|bottom" />
</RelativeLayout>

和工具栏xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:elevation="4dp">
</android.support.v7.widget.Toolbar>

错误消息

02-22 09:09:07.835 12623-12623/sk.cassomedia.studentassitent E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           java.lang.ClassCastException: sk.cassomedia.studentassitent.MainActivity cannot be cast to android.support.v7.app.ActionBarActivity
                                                                               at sk.cassomedia.studentassitent.view.Contacts.onCreateView(Contacts.java:30)
                                                                               at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                                                                               at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                               at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                                                                               at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                                               at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
                                                                               at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                                               at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
                                                                               at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
                                                                               at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
                                                                               at android.support.v4.view.ViewPager$3.run(ViewPager.java:251)
                                                                               at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1849)
                                                                               at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:1978)
                                                                               at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2033)
                                                                               at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
                                                                               at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2091)
                                                                               at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
                                                                               at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2091)
                                                                               at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
                                                                               at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2091)
                                                                               at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2306)
                                                                               at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1575)
                                                                               at android.app.Activity.dispatchTouchEvent(Activity.java:2470)
                                                                               at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2254)
                                                                               at android.view.View.dispatchPointerEvent(View.java:7888)
                                                                               at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3977)
                                                                               at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3861)
                                                                               at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5101)
                                                                               at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5080)
                                                                               at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5179)
                                                                               at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                                                                               at android.os.MessageQueue.nativePollOnce(Native Method)
                                                                               at android.os.MessageQueue.next(MessageQueue.java:125)
                                                                               at android.os.Looper.loop(Looper.java:138)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5365)
                                                                               at java.lang.reflect.Method.invokeNative(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:511)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
                                                                               at dalvik.system.NativeStart.main(Native Method)

styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

MainActivity

package sk.cassomedia.studentassitent;

import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;


import sk.cassomedia.studentassitent.controllers.FragmentAdapter;

public class MainActivity extends FragmentActivity {

    static final int NUMBER_OF_SCREENS = 3;

    private FragmentAdapter adapter;
    private ViewPager viewPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        adapter = new FragmentAdapter(getSupportFragmentManager());
        viewPage = (ViewPager) findViewById(R.id.pager);
        viewPage.setAdapter(adapter);
    }
}

如果我更改主要活动以扩展AppCompat活动

,则记录消息
02-22 09:15:26.757 14192-14192/sk.cassomedia.studentassitent E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           java.lang.ClassCastException: sk.cassomedia.studentassitent.MainActivity cannot be cast to android.support.v7.app.ActionBarActivity
                                                                               at sk.cassomedia.studentassitent.view.Contacts.onCreateView(Contacts.java:30)
                                                                               at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                                                                               at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                               at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                                                                               at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                                               at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
                                                                               at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                                               at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
                                                                               at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
                                                                               at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
                                                                               at android.support.v4.view.ViewPager$3.run(ViewPager.java:251)
                                                                               at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1849)
                                                                               at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:1978)
                                                                               at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2033)
                                                                               at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
                                                                               at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2091)
                                                                               at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
                                                                               at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2091)
                                                                               at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
                                                                               at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2091)
                                                                               at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
                                                                               at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2091)
                                                                               at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
                                                                               at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2091)
                                                                               at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2306)
                                                                               at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1575)
                                                                               at android.app.Activity.dispatchTouchEvent(Activity.java:2470)
                                                                               at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
                                                                               at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2254)
                                                                               at android.view.View.dispatchPointerEvent(View.java:7888)
                                                                               at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3977)
                                                                               at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3861)
                                                                               at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5101)
                                                                               at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5080)
                                                                               at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5179)
                                                                               at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                                                                               at android.os.MessageQueue.nativePollOnce(Native Method)
                                                                               at android.os.MessageQueue.next(MessageQueue.java:125)
                                                                               at android.os.Looper.loop(Looper.java:138)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5365)
                                                                               at java.lang.reflect.Method.invokeNative(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:511)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
                                                                               at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

尝试将MainActivity父类更改为AppCompatActivity