开始片段事务时非常简单的代码崩溃
启动时崩溃
如果我评论交易行
,则有效主要活动:
package com.example.damia.radio.activities;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.damia.radio.R;
import com.example.damia.radio.fragments.MainFragment;
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
MainFragment mainFragment = (MainFragment)fm.findFragmentById(R.id.fragcontainer);
if(mainFragment == null) {
mainFragment = MainFragment.newInstance("", "");
fm.beginTransaction().add(R.id.fragcontainer, mainFragment).commit();
}
}
}
主要活动XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.damia.radio.activities.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/fragcontainer">
</FrameLayout>
</RelativeLayout>
片段类和XML是默认的,没有触及它们