android - 使用 Android 5.0 工具栏小部件的 fragment 未显示在 ActionBarActivity 中

标签 android android-fragments android-5.0-lollipop

当我运行我的应用程序时,MainFragment 根本没有出现在扩展 ActionBarActivity 的 Activity 中。我检查了this post ,但这并没有帮助我解决我的问题。这是我的 Activity 和 fragment :

activity_main.xml:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new MainFragment())
                .commit();
    }

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
  }
}

fragment_main.xml:

public class MainFragment extends Fragment {

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

我认为我不需要在此处引用任何 xml...一切正常,直到我扩展 ActionBarActivity 而不是 activity_main.xml 中的 Activity。

编辑:添加 xml 以防万一:

activity_main.xml:

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

<include layout="@layout/toolbar" />

</LinearLayout>

fragment_main.xml:

<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"
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$PlaceholderFragment">

// There are several text views and buttons I will not add to this code, 
// but just know that this fragment isn't blank.

</RelativeLayout>

编辑 2:我做了一些测试,每当我从 activity_main.xml 中删除工具栏时, fragment 就会出现,并且没有操作栏(因为这是我为我的应用程序设置的主题,以便使用工具栏)。所以这也是我的 toolbar.xml 的 xml,我在 activity_main.xml 中使用它。也许我做错了什么,它隐藏了我的整个 fragment ?

tl;dr: fragment 显示何时从 activity_main.xml 中删除了这行代码:

<include layout="@layout/toolbar" />

还有这个在 activity_main.xml 中:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

最佳答案

刚开了一个bug report at AOSP .我现在找到并使用的解决方案是在 post() 中调用 add()

new Handler().post(new Runnable() {
    @Override
    public void run() {
        getFragmentManager().beginTransaction()
            .add(R.id.container, new MainFragment())
            .commit();
    }
});

关于android - 使用 Android 5.0 工具栏小部件的 fragment 未显示在 ActionBarActivity 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26727488/

相关文章:

java - 在 Android 上序列化复杂对象 (Java)

android - Xamarin Forms Android 问题,包 android.support.design.* 不存在

Android Viewpager 在第一次加载时显示错误数据

android - 如何在 Android 中为 fragment 制作单独的返回堆栈

android - "You cannot install this app because another user has already installed an incompatible version on this device"

android - 如何提取 Lollipop 版状态栏中的通知文本?

android - 安装后服务不启动?

android - Cordova 自适应图标导致构建失败 : The "path" argument must be of type string

java - "Modifier static not allowed"尝试将 fragment 连接到 Activity 时

Android如何在扩展 fragment 的类中将json解析为listtview