活动启动时的java默认片段
我试图加载一个片段作为活动的默认视图,但我只得到一个空白屏幕和巨大的内存泄漏
活动文件onCreate
方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if (savedInstanceState == null) {
FragmentManager fragmentManager = getFragmentManager();
int fragmentTransaction = fragmentManager.beginTransaction()
.add(R.id.login_screen_fragment, new FragmentLogin())
.commit();
}
}
活动的XML:
<FrameLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context=".LoginActivity">
<fragment
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:id="@+id/login_screen_fragment"
class="com.test.project.FragmentLogin"
/>
<fragment
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:id="@+id/forgotten_password_fragment"
class="com.test.project.FragmentForgottenPassword"
/>
</FrameLayout>
以及片段类(相关部分):
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_login, container, false);
final Activity activity = getActivity();
... code ...
return view;
}
我遵循了之前有人从另一个问题中建议的教程的说明,但我一定是做错了什么。有什么想法吗
共 (0) 个答案