我决定从多个 Activity 切换到一个 Activity ,该 Activity 在 fragment 之间切换,但应用程序现在崩溃了。
这是我要添加 fragment 的 Activity
公共(public)类 MainActivity 扩展了 SherlockFragmentActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyFragment fragment = new MyFragment();
fragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, fragment).commit();
}
这是一个观察者的 fragment ,具有功能,但为了节省空间,我只显示创建
public class MyFragment extends SherlockFragment implements Observer{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.my_fragment, container, false);
}
这是 my_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
... HAS SOME TEXT VIEWS HERE!
</RelativeLayout>
我遇到的崩溃是
02-15 16:17:41.079: E/AndroidRuntime(18668): FATAL EXCEPTION: main
02-15 16:17:41.079: E/AndroidRuntime(18668): java.lang.RuntimeException:
Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.MainActivity}:
java.lang.IllegalArgumentException:
No view found for id 0x7f040036 for fragment MyFragmentt{41a05910 #0 id=0x7f040036}
有人可以帮我吗?我真的无法弄清楚是什么原因造成的。我知道如果我在主 Activity (本文中的顶部代码块)中注释掉 getSupportFragmentManager() ,它将运行,但不会在我的 fragment 中绘制任何内容。
更新
我不确定放置在哪里的frame_container
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
最佳答案
因此,您可以在应用程序中使用 Fragments
的方式有两种。
第一种方法是在 xml 文件中声明 Fragment,如下所示:
<fragment android:name="com.example.news.ArticleReaderFragment"
android:id="@+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
第二种方法是,如果您将 Fragments 动态添加/替换到容器中,在大多数示例中是 FrameLayout
。以下是您可以执行此操作的方法:
在你的主要FragmentActivity
中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myFragmentContainer);
}
在您的 xml myFragmentContainer.xml
中,您放置 fragment_container
,它看起来像:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
并且您正在添加和替换您的Fragments
,如下所示:
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
// Create an instance of ExampleFragment
HeadlinesFragment firstFragment = new HeadlinesFragment();
// if there are any extras
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
对于您想要显示的下一个 fragment ,只需执行以下操作:
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, secondFragment).commit();
关于java - Android 在 getSupportFragmentManager() 处崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14899122/