デバイスが回転しているときにフラグメントに問題があります: 5 つのフラグメントがあります:
- メニューは最初のフラグメントで、アクティビティが開始されると起動されます。
- AddUser (メニュー フラグメントのボタンを押すと表示される単純なフラグメント)
- DetailApp (メニュー フラグメントのボタンを押すと表示される単純なフラグメント)
- DetailUser (メニュー フラグメントのボタンを押すと表示される単純なフラグメント)
- ListUser (メニュー フラグメントのボタンを押すと表示される ListFragment)
垂直モードでアクティビティを開始すると、フラグメントが正しく表示されます。スマートフォンを回転させると、すべてのフラグメントが重なっていることがわかります。ListUser を起動すると、次のようになります。
IllegalArgumentException: No view found for id 0x... for fragment ...
メニューフラグメントを開始するとき:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manager);
これはオンクリエイトです
// Check whether the activity is using the layout version with
// the fragment_container FrameLayout. If so, we must add the first fragment
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
//first frame show a menu
Menu firstFragment = new Menu();
// In case this activity was started with special instructions from an Intent,
// pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
開始時のフラグメント コンテナーは無効です。
<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" />
ボタンを押してフラグメントの1つを開始すると:
@Override
public void onDetailApp(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Capture the article fragment from the activity layout
DetailApp appFrag = (DetailApp)
getSupportFragmentManager().findFragmentById(R.id.detailapp_fragment);
if (appFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
appFrag.updateAppointmentView(position);
} else {
// If the frag is not available, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
DetailApp newFragment = new DetailApp();
Bundle args = new Bundle();
args.putInt(DetailApp.ARG_APPOINTMENT, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
これは、DetailUser、AddUser、DetailApp からの onCreateView です。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// If activity recreated (such as from screen rotate), restore
// the previous article selection set by onSaveInstanceState().
// This is primarily necessary when in the two-pane layout.
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
}
// Inflate the layout for this fragment
return inflater.inflate(R.layout.detail_adduser, container, false);
}
誰が私を助けることができます?