没有查看发现ID时的方向改变抛出:IllegalArgumentException:?(No Vie

2019-09-24 03:39发布

我创建的应用程序。 我有两种观点。

ThumbView和GridView控件。 对于这些意见,我使用ViewPager两种不同FragmentStatePagerAdapters。 随着主要布局,我创建了一个空间重用子视图基础上,选择加载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一个subLayout内景

但是,当我改变方向,然后应用程序被越来越崩溃,因为在上面的代码行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

小碎片是一个扩展片段类的名称。 如何来处理呢? 如何有力地摧毁/分离的片段? 如何彻底删除ViewPager和一些操作的所有与其相关的片段?

Answer 1:

您可以覆盖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();
    }
}
  • 这里更多信息http://developer.android.com/guide/topics/resources/runtime-changes.html


文章来源: No View found for id when orientation changes IllegalArgumentException:?