简体   繁体   English

android.support.v4.app.FragmentActivity缺少操作栏

[英]android.support.v4.app.FragmentActivity missing action bar

I am using the support library android.support.v4.app.FragmentActivity to create Fragment based activities. 我正在使用支持库android.support.v4.app.FragmentActivity创建基于Fragment的活动。

Since Lollipop I am now missing my action bar in all fragment based activities. 从棒棒糖开始,我现在在所有基于片段的活动中都缺少动作栏。 I am wondering is this something to do with activities now inheriting from actionbaractivity? 我想知道这与现在从actionbaractivity继承的活动有关吗?

I list below my manifest, activity and fragment xml and hope someone can tell me why this is happening. 我在清单,活动和片段xml下方列出,希望有人可以告诉我为什么会这样。 The design screens all show the title bar but it is missing on emulator and actual device runs. 设计屏幕均显示标题栏,但在仿真器和实际设备运行中缺少标题栏。

First of all here is my fragment activity class that I extend for each activity, followed by my xml files: 首先,这是我为每个活动扩展的片段活动类,然后是我的xml文件:

SingleFragmentActivity.java: SingleFragmentActivity.java:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

public abstract class SingleFragmentActivity extends FragmentActivity {

    protected abstract Fragment createFragment();

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

        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);

        if (fragment == null) {
            fragment = createFragment();
            fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit();
        }
    }

}

AndroidManifest.xml: AndroidManifest.xml中:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="safecontact.net.savemenow" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_fragment.xml: activity_fragment.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/fragmentContainer"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</FrameLayout>

fragment_appt.xml: fragment_appt.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:weightSum="1">
... REST OF FRAGMENT LAYOUT CUT FOR BREVITY

In design view the action bar and title etc are clearly visible, but when I run on the emulator the action bar is totally missing and only the activity view (with fragment) is rendered. 在设计视图中,操作栏和标题等清晰可见,但是当我在模拟器上运行时,操作栏完全丢失,仅渲染活动视图(带有片段)。

I hope someone can help with this as it is driving me mad. 我希望有人能对此有所帮助,因为这使我发疯。 I am convinced it is something to do with the fact that I am using the support library for my fragments. 我确信这与我使用片段支持库有关。 Or maybe it is a theme issue? 也许这是一个主题问题?

Thanks. 谢谢。

EDIT: styles.xml listed as requested: 编辑:按要求列出styles.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

</resources>

.

Your AppTheme is inheriting from Theme.AppCompat . 您的AppTheme是继承自Theme.AppCompat For that to work, you must inherit from AppCompatActivity . 为此,您必须继承自AppCompatActivity Since AppCompatActivity itself inherits from FragmentActivity , most of your existing code should be fine. 由于AppCompatActivity本身是从FragmentActivity继承的,因此大多数现有代码都可以。 The only stuff that would need to change is anything related to the action bar, such as: 唯一需要更改的内容是与操作栏相关的任何内容,例如:

  • Use getSupportActionBar() , rather than getActionBar() , to get the ActionBar object 使用getSupportActionBar()而不是getActionBar()来获取ActionBar对象

  • Use attributes in a custom namespace (eg, app ) where needed (eg, app:showAsAction ) instead of the android namespace (eg, android:showAsAction ) in menu resources 在需要的地方(例如app:showAsAction )使用自定义名称空间(例如app )中的属性,而不是菜单资源中的android名称空间(例如android:showAsAction

Note that in older versions ActionBarActivity had been used, which has been declared deprecated. 请注意,在较早的版本中,已使用ActionBarActivity ,但已声明该方法已弃用。

将支持工具栏用于Lollipop而不是ActionBar

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谷歌地图v2,缺少&#39;android.support.v4.app.FragmentActivity&#39; - Google maps v2, missing 'android.support.v4.app.FragmentActivity' android.support.v4.app.FragmentActivity扩展了什么? - What does android.support.v4.app.FragmentActivity extend? android.support.v4.app.FragmentActivity无法解析 - android.support.v4.app.FragmentActivity can not be resolved 无法在ViewModelProviders中解析android.support.v4.app.FragmentActivity的方法 - Cannot resolve method of android.support.v4.app.FragmentActivity in ViewModelProviders 无法将ContraceptiveFragment中的ContraceptiveFragment()应用于(android.support.v4.app.FragmentActivity) - ContraceptiveFragment( ) in ContraceptiveFragment cannot be applied to (android.support.v4.app.FragmentActivity) “TAG”在“android.support.v4.app.FragmentActivity”中有私有访问权限 - 'TAG' has private access in 'android.support.v4.app.FragmentActivity' “无法找到类'android.support.v4.app.FragmentActivity'”错误 - “Could not find class 'android.support.v4.app.FragmentActivity'” Error 无法解析构造函数&#39;GridLayoutManager(android.support.v4.app.FragmentActivity) - Cannot resolve constructor 'GridLayoutManager(android.support.v4.app.FragmentActivity) 无法将clcik上的android.app.Application强制转换为android.support.v4.app.FragmentActivity - android.app.Application cannot be cast to android.support.v4.app.FragmentActivity on clcik 被android.support.v4.app.FragmentActivity和android.app.FragmentTransaction混淆 - Confused by android.support.v4.app.FragmentActivity and android.app.FragmentTransaction
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM