没有查看发现ID时,方向更改抛出:IllegalArgumentException:? [英] No View found for id when orientation changes IllegalArgumentException:?
问题描述
我创建的应用程序。我有两种看法。
I am creating an application. I have two kinds of view.
ThumbView和放大器;网格视图。对于这些观点,我使用两种不同的FragmentStatePagerAdapters为ViewPager。随着主要布局,我创建了一个空间,以重用子视图基础上,选择要加载ThumbView或GridView的。
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);
}
这是我在做什么加载在MainLayout.xml一个subLayout
This is what I am doing to load view inside a subLayout in MainLayout.xml
但是,当我改变了方向,那么应用程序是越来越坠毁,因为在第2行中的上述code,我删除所有意见。当我转动我的应用程序,然后我的应用程序是越来越崩溃并出现以下错误。
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
亚片段是一个扩展片段的类的名称。 如何处理呢? 如何有力地摧毁/分离的片段? 如何彻底删除ViewPager和放大器;对于一些行动的所有其相关的片段?
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?
推荐答案
您可以覆盖 onConfigurationChanged(...)
处理方向改变。
You can override onConfigurationChanged(...)
to handle orientation changes.
将此行添加到您的<活性GT;
在mainfest:
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();
}
}
- 点击此处了解详情 http://developer.android.com/guide /topics/resources/runtime-changes.html
- More information here http://developer.android.com/guide/topics/resources/runtime-changes.html
这篇关于没有查看发现ID时,方向更改抛出:IllegalArgumentException:?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!