3

我正在创建一个应用程序。我有两种看法。

拇指视图和网格视图。对于这些视图,我为 ViewPager 使用了两个不同的 FragmentStatePagerAdapter。在主布局中,我创建了一个空间来重用该子视图以根据选择加载 ThumbView 或 GridView。

public void loadThumbView()
{
     LinearLayout subLayout= (LinearLayout)findViewById(R.id.subLayout);
           subLayout.removeAllViews();
           View subView =(View)inflater.inflate(R.layout.thumb_layout, null);
           thumbViewPager=(ViewPager)subView.findViewById(R.id.pager);
           thumbViewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());
           thumbViewPager.setAdapter(thumbViewPagerAdapter);
           subLayout.addView(subView);
}

public void loadGridView()
{
LinearLayout subLayout= (LinearLayout)findViewById(R.id.subLayout);
        subLayout.removeAllViews();     
        View subView=(View)inflater.inflate(R.layout.grid_layout, null);
        test= new TestAdapter(getSupportFragmentManager(),this);
        viewPager =(ViewPager)subView.findViewById(R.id.pager);        
        viewPager.setAdapter(test);        
        pageIndicator = (CirclePageIndicator)subView.findViewById(R.id.indicator);        
        pageIndicator.setViewPager(viewPager,0);        
        pageIndicator.setSnap(false);
        subLayout.addView(subView);
}

这就是我在 MainLayout.xml 的子布局中加载视图的方法

但是,当我更改方向时,应用程序会崩溃,因为在上述代码的第 2 行,我正在删除所有视图。当我旋转我的应用程序时,我的应用程序因以下错误而崩溃。

10-15 19:50:43.146: E/AndroidRuntime(8546): FATAL EXCEPTION: main
10-15 19:50:43.146: E/AndroidRuntime(8546): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.taruni.testsample/com.taruni.activities.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f04000a for fragment SubFragment{41579be0 #0 id=0x7f04000a}
10-15 19:50:43.146: E/AndroidRuntime(8546):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3512)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at android.app.ActivityThread.access$700(ActivityThread.java:130)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at android.os.Looper.loop(Looper.java:137)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at java.lang.reflect.Method.invoke(Method.java:511)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at dalvik.system.NativeStart.main(Native Method)
10-15 19:50:43.146: E/AndroidRuntime(8546): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f04000a for fragment SubFragment{41579be0 #0 id=0x7f04000a}
10-15 19:50:43.146: E/AndroidRuntime(8546):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:864)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1810)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:501)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at com.taruni.activities.MainActivity.onStart(MainActivity.java:374)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at android.app.Activity.performStart(Activity.java:5018)
10-15 19:50:43.146: E/AndroidRuntime(8546):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
10-15 19:50:43.146: E/AndroidRuntime(8546):     ... 12 more

SubFragment 是扩展 Fragment 的类的名称。如何处理?如何强行销毁/分离碎片?如何完全删除 ViewPager 及其所有相关的片段以进行某些操作?

4

1 回答 1

0

您可以覆盖onConfigurationChanged(...)以处理方向更改。

将此行添加到您<activity >的 mainfest 中:

android:configChanges="orientation"

添加此方法以在更改方向时执行您想要的操作

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}
于 2012-10-15T18:51:48.297 回答