Android L - Activity with Fragment 显示错误(只显示 fragment )

标签 android android-fragments android-5.0-lollipop material-design

正如标题所说,当我尝试使用 fragment 进行简单 Activity 时,屏幕上显示的只是 fragment 内容。状态栏,背景,一切都不见了。 I made a video to show what happens [0:47] .如果您观看视频,该应用程序可以在 KitKat 上运行,但在 Lollipop 上会失败。我不确定这是否是 L 预览失败。

我的 MainActivity 的代码是由 Android Studio 生成的:

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle prevState)
    {
        super.onCreate(prevState);
        setContentView(R.layout.activity_main);

        if(prevState == null)
            getFragmentManager().beginTransaction().replace(R.id.main_fragment, new PlaceholderFragment()).commit();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if(id == R.id.action_settings)
            return true;

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment
    {

        public PlaceholderFragment()
        {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle prevState)
        {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
}

我的 .xml 文件只是一个 FrameLayout,里面有一个 fragment 元素。它在 onCreate(Bundle prevState) 方法中被替换,如上所示。

这是我的 MainActivity XML 文件:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" >

    <fragment
        android:id="@+id/main_fragment"
        android:name="me.kworden.dart.fragment.MainFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        tools:layout="@layout/fragment_main" />

</FrameLayout>

我的风格是这样的:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
        <item name="android:colorPrimaryDark">#C40048</item>
        <item name="android:colorPrimary">#FF005D</item>
        <item name="android:colorAccent">#AEFF00</item>
    </style>
</resources>

有什么想法吗?

最佳答案

您应该尝试删除 fragment ,然后添加新 fragment ,看看是否失败。 我也会尝试在 onCreate 之后稍后进行替换。

顺便说一句,为什么你仍然使用 L 预览,而不是 L ?

此外,如果 id 可以是 fragment 本身的 id,我也不是很清楚。文档似乎更多地指代容器 ID 而不是 fragment ID...

关于Android L - Activity with Fragment 显示错误(只显示 fragment ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26986257/

相关文章:

android - 从保存的实例恢复 fragment 时如何防止 editText 自动填充文本?

android - 为什么 findFragmentById 返回调用替换 Fragment Layout 之前的旧 fragment ?

android - 不同的口味没有安装在同一个安卓设备上

android - 如何使用Android Studio在Android L或4.4上运行应用程序?

android - 将纹理快速加载到 OpenGL 2.0

android - 如果将 "developer payload"留空会发生什么 Google Play 应用内结算

android - 当我的应用程序在 Android 中运行时,如何打开 Wifi?

android - 如何更改 Android 中 tabStrip 的颜色?

android - 如何将滑动选项卡 View 放入抽屉导航选项之一

Wi-Fi无法上网时Android Lollipop默认使用Mobile Data?