gpt4 book ai didi

android - 方向更改时找不到 id 的 View IllegalArgumentException :?

转载 作者:太空狗 更新时间:2023-10-29 15:15:50 26 4
gpt4 key购买 nike

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

ThumbView 和 GridView。对于这些 View ,我为 ViewPager 使用了两个不同的 FragmentStatePagerAdapter。在主布局中,我创建了一个空间来重用该 subview 以根据选择加载 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 中加载 View

但是,当我改变方向时,应用程序崩溃了,因为在上面代码的第 2 行,我删除了所有 View 。当我旋转我的应用程序时,我的应用程序因以下错误而崩溃。

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 的类名。如何处理?如何强行销毁/分离 fragment ?如何完全删除 ViewPager 及其所有相关 fragment 以进行某些操作?

最佳答案

您可以覆盖 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();
}
}

关于android - 方向更改时找不到 id 的 View IllegalArgumentException :?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12900129/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com