2

我的设备旋转时出现碎片问题:我有 5 个碎片:

  1. 菜单是第一个片段,当活动开始时,它被启动。
  2. AddUser(当我按下菜单片段中的按钮时可以看到的简单片段)
  3. DetailApp(当我按下菜单片段中的按钮时可以看到的简单片段)
  4. DetailUser(当我按下菜单片段中的按钮时可以看到的简单片段)
  5. 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);

这是 oncreate

// 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" />

当我按下一个按钮并且其中一个片段开始时:

@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);
}

谁能帮我?

4

2 回答 2

-1

我将此添加到我的清单活动并覆盖 onConfigurationChanged: android:configChanges="keyboardHidden|orientation|screenSize"

    <activity
        android:name="com.map.manager.ManagerActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label="@string/title_activity_manager" >
    </activity>

我没有更多的问题了!我可以看到正确的垂直和水平布局(当我旋转智能手机时)。但我发现了这一点:为什么 我必须将所有可能的配置更改都放入“黑名单”中?区域设置触摸屏屏幕尺寸等?

于 2013-01-12T08:44:59.500 回答
-1

代替

 getSupportFragmentManager().beginTransaction()
 .add(R.id.fragment_container, firstFragment).commit();  

经过

 getSupportFragmentManager().beginTransaction()
 .replace(R.id.fragment_container, firstFragment).commit();  

99% 的时间它解决了重复问题。

于 2013-01-11T14:28:03.460 回答