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

これはオンクリエイトです

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

誰が私を助けることができます?

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>

そして私はこれ以上問題はありません!縦横のレイアウトが正しく見えます(スマートフォンを回転させたとき)。しかし、私はこれを見つけました:なぜ-not-use-always-androidconfigchanges は、考えられるすべての構成変更を「ブラックリスト」に入れる必要がありますか?ロケールタッチスクリーンscreenSizeなど?

于 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 に答える