当我运行我的应用程序时,MainFragment根本没有显示在扩展ActionBarActivity的活动中。我确实检查了this post,但它并没有帮我解决问题。这是我的活动和片段:
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 ......一切正常,直到我在activity_main.xml中扩展ActionBarActivity而不是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中删除工具栏时,片段都会显示,并且没有操作栏(因为这是我按顺序设置到我的应用程序的主题使用工具栏)。所以这里是我的toolbar.xml的xml,我在activity_main.xml中使用它。也许我为了隐藏我的整个片段做错了什么?
tl; dr:fragment显示从activity_main.xml中删除此代码行的时间:
<include layout="@layout/toolbar" />
和activity_main.xml中的这个:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
答案 0 :(得分:5)
刚开了bug report at AOSP。我现在找到并使用的解决方案是在add()
内调用post()
。
new Handler().post(new Runnable() {
@Override
public void run() {
getFragmentManager().beginTransaction()
.add(R.id.container, new MainFragment())
.commit();
}
});
答案 1 :(得分:2)
您需要将getfragmentManager()更改为getSupportFragmentManager()
喜欢这个
getFragmentManager().beginTransaction()
.add(R.id.container, new MainFragment())
.commit();
到
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new MainFragment())
.commit();
因为ActionBarActivtiy在supportpackage中所以你需要getSupportFragmentManager()
答案 2 :(得分:1)
也许您实际上并未使用v7 appcompat库,在这种情况下,MainActivity
应该扩展FragmentActivity
?另见:Fragment add or replace not working
public class MainActivity extends FragmentActivity {
@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);
setActionBar(toolbar);
}
...
}
或者,如果您使用的是v7 appcompat库,
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new MainFragment())
.commit();
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
...
}
答案 3 :(得分:0)
尝试这样可以帮助你,
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().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);
}
}
答案 4 :(得分:0)
首先检查import
中的MainFragment
。我确定它是
import android.app.Fragment;
来自Fragment
support library
import android.support.v4.app.Fragment;
在MainACtivity
而不是
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new MainFragment())
.commit();
}
使用
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new MainFragment())
.commit();
检查清单文件,如果minSdkVersion
7 或 8 ,如果它大于 10 然后使用Activity
Fragment
和android.app.Fragment
android:minSdkVersion="8"
您必须正确添加appcompat v21
或appcompat v7
库,并使用您添加的库中的Theme
。
答案 5 :(得分:0)
您的工具栏位于您的片段之上。将找到的工具栏视图设置为ActionBar很可能会导致布局上的另一个传递,这会使片段视图消失(很可能在工具栏下被压扁)。
编辑:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@id/toolbar"
android:align_parentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@id/content"
android:layout_below="@id/toolbar"
android:align_parentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
答案 6 :(得分:0)
这个问题已经解决了,但它并没有为我工作。
所以我会发布我的解决方法。我只是禁用工具栏的抽屉指示器并再次启用它。因此工具栏会自动显示箭头。在onCreate(..)
中尝试以下代码 supportFragmentManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
int stackHeight = supportFragmentManager.getBackStackEntryCount();
ActionBar supportActionBar = getSupportActionBar();
if (stackHeight > 0) {
mDrawerToggle.setDrawerIndicatorEnabled(false);
} else {
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerToggle.syncState();
}
}
});