【问题标题】:Android L - Activity with Fragment display error (only the fragment is displayed)Android L - Activity with Fragment 显示错误(仅显示片段)
【发布时间】:2015-01-15 03:58:39
【问题描述】:

正如标题所说,当我尝试使用片段制作一个简单的活动时,我在屏幕上看到的只是片段内容。状态栏,背景,一切都不见了。 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>

有什么想法吗?

【问题讨论】:

  • 您将片段的 ID 提供给 FragmentTransaction#replace 调用。应该是容器的IDR.id.main_container

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


【解决方案1】:

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

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

另外,我不太清楚 id 是否可以是片段本身的 id。文档似乎引用容器 ID 多于片段 ID...

【讨论】:

  • 当我可以访问我的计算机时,我会尝试一下,谢谢
  • 切换到 L 版本,使用 main_container 而不是 main_fragment 后一切正常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多