No View found for id when orientation changes IllegalArgumentException:?

拟墨画扇 提交于 2020-01-04 04:39:08

问题


I am creating an application. I have two kinds of view.

ThumbView & GridView. For these views I am using two different FragmentStatePagerAdapters for ViewPager. With in the main layout, I have created a space to reuse that subview to load ThumbView or GridView based on the selection.

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

This is what I am doing to load view inside a subLayout in MainLayout.xml

But, when I change the orientation, then app is getting crashed because, at line 2 in the above code, I am removing all Views. When I rotate my app, then my app is getting crashed with the following error.

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 is the name of the class that Extends Fragment. How to handle this? How to forcefully destroy/ detach fragments? How to completely remove ViewPager & all its associated Fragments for some action?


回答1:


You can override onConfigurationChanged(...) to handle orientation changes.

Add this line to your <activity > in your mainfest:

android:configChanges="orientation"

Add this method to do what you want when the orientation is changed

@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();
    }
}
  • More information here http://developer.android.com/guide/topics/resources/runtime-changes.html


来源:https://stackoverflow.com/questions/12900129/no-view-found-for-id-when-orientation-changes-illegalargumentexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!