cod*_*e22 265 android illegalargumentexception android-fragments
我有一个片段,我试图添加到视图中.
FragmentManager fragMgr=getSupportFragmentManager();
feed_parser_activity content = (feed_parser_activity)fragMgr
.findFragmentById(R.id.feedContentContainer);
FragmentTransaction xaction=fragMgr.beginTransaction();
if (content == null || content.isRemoving()) {
content=new feed_parser_activity(item.getLink().toString());
xaction
.add(R.id.feedContentContainer, content)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null)
.commit();
Log.e("Abstract", "DONE");
}
Run Code Online (Sandbox Code Playgroud)
执行此代码时,我在调试中收到以下错误.
java.lang.IllegalArgumentException: No view found for id 0x7f080011
for fragment feed_parser_activity{41882f50 #2 id=0x7f080011}
Run Code Online (Sandbox Code Playgroud)
feed_parser_activity是一个片段,在xml中设置为Fragment layout.
我正在使用FragmentActivity来托管片段布局feed_parser_layout.
我上面正确编码吗?
how*_*ttl 350
我也遇到了这个问题,直到我意识到我在FragmentActivity setContentView()的onCreate()方法中指定了错误的布局.
FragmentTransaction.add()在您的情况下R.id.feedContentContainer,传入的id 必须是指定布局的子级setContentView().
你没有告诉我们你的onCreate()方法,所以也许这是同样的问题.
Mal*_*asz 266
嵌套Fragments并使用getSupportFragmentManager()而不是getChildFragmentManager()添加它们时也会发生此错误.
Ste*_*iaz 62
解决方案是使用 getChildFragmentManager()
代替 getFragmentManager()
从片段调用时 如果从活动中调用方法,则使用getFragmentManager().
这将解决问题.
zdd*_*zdd 27
我见过的另一个场景.如果您使用嵌套片段,请说片段中的ViewPager,其页面也是片段.
当您在内部片段(ViewPager的页面)中执行Fragment事务时,您将需要
FragmentManager fragmentManager = getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Run Code Online (Sandbox Code Playgroud)
getActivity()是这里的关键....
gar*_*bay 14
在我的情况下,我试图显示包含寻呼机的DialogFragment,当FragmentPagerAdapter尝试将碎片添加到寻呼机时抛出此异常.根据howettl的回答,我猜这是由于Pager父节点不是我的FragmentActivity中的setContentView()中的视图集.
我为解决这个问题所做的唯一改变是创建FragmentPagerAdapter传入一个通过调用getChildFragmentManager()获得的FragmentMager,而不是像往常一样调用getFragmentManager()获得的那个.
public class PagerDialog extends DialogFragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.pager_dialog, container, false);
MyPagerAdapter pagerAdapter = new MyPagerAdapter(getChildFragmentManager());
ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager);
pager.setAdapter(pagerAdapter);
return rootView;
}
}
Run Code Online (Sandbox Code Playgroud)
}
小智 12
如果要传递的布局ID FragmentTransaction.replace(int ID, fragment)存在于其他正在膨胀的布局中,也会发生此异常.确保布局ID是唯一的,它应该工作.
sco*_*eus 12
我在另一个类似于这个问题的线程上读到的答案,当我遇到这个问题时,这个问题涉及布局xml.
你的logcat说"找不到id 0x7f080011的视图".
打开gen->package->R.java->id然后找id 0x7f080011.
当我遇到这个问题时,这个id属于FrameLayout我的activity_main.xml文件中的一个.
FrameLayout没有ID(没有声明android:id = "blablabla").
确保所有布局中的所有组件都具有ID,尤其是logcat中引用的组件.
ono*_*ono 11
我升级com.android.support:support-v4:21.0.0到时出现此错误com.android.support:support-v4:22.1.1.
我不得不改变我的布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container_frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
对此:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/container_frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
所以布局必须有一个子视图.我假设他们在新库中强制执行此操作.
我知道这已经在一个场景中得到了解答,但我的问题略有不同,我想我会分享以防其他任何人在我的鞋子里.
我正在进行交易onCreate(),但此时视图树没有被夸大,所以你得到同样的错误.将事务代码放入onResume()使得一切运行正常.
因此,只需确保在视图树膨胀后运行您的事务代码!
在Recycler View中使用Viewpager时,我遇到了一个令人讨厌的错误.我在特殊情况下面临的错误.我创建了一个带有Viewpager的RecyclerView的片段(使用FragmentStatePagerAdapter).它运行良好,直到我在RecyclerView中单击一个Cell切换到不同的片段,然后使用Phone的硬件Back按钮导航回来,App崩溃了.
有趣的是,我在同一个RecyclerView中有两个Viewpage,两个距离大约5个单元(其他在屏幕上看不到,它已经关闭).所以最初我只是将解决方案应用于第一个Viewpager并保留其他原样(Viewpager使用Fragments).
当第一个查看寻呼机可见时,向后导航工作正常.现在,当我向下滚动到第二个然后更改片段并返回时,它崩溃了(同样的事情发生在第一个).所以我不得不改变两个Viewpagers.
无论如何,请阅读下面的工作解决方案.崩溃错误如下:
java.lang.IllegalArgumentException: No view found for id 0x7f0c0098 (com.kk:id/pagerDetailAndTips) for fragment ProductDetailsAndTipsFragment{189bcbce #0 id=0x7f0c0098}
Run Code Online (Sandbox Code Playgroud)
花了几个小时调试它.阅读完整的主题帖子,直到底部应用所有解决方案,包括确保我传递childFragmentManager.
没有任何效果.
最后,我没有使用FragmentStatePagerAdapter,而是扩展了PagerAdapter并在Viewpager中使用它而不使用片段.我相信一些有嵌套片段的BUG.无论如何,我们有选择.阅读......
以下链接非常有用:
链接可能会死,所以我在下面发布我实现的解决方案:
public class ScreenSlidePagerAdapter extends PagerAdapter {
private static final String TAG = "ScreenSlidePager";
ProductDetails productDetails;
ImageView imgProductImage;
ArrayList<Imagelist> imagelists;
Context mContext;
// Constructor
public ScreenSlidePagerAdapter(Context mContext,ProductDetails productDetails) {
//super(fm);
this.mContext = mContext;
this.productDetails = productDetails;
}
// Here is where you inflate your View and instantiate each View and set their values
@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.product_image_slide_cell,container,false);
imgProductImage = (ImageView) layout.findViewById(R.id.imgSlidingProductImage);
String url = null;
if (imagelists != null) {
url = imagelists.get(position).getImage();
}
// This is UniversalImageLoader Image downloader method to download and set Image onto Imageview
ImageLoader.getInstance().displayImage(url, imgProductImage, Kk.options);
// Finally add view to Viewgroup. Same as where we return our fragment in FragmentStatePagerAdapter
container.addView(layout);
return layout;
}
// Write as it is. I don't know much about it
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
/*super.destroyItem(container, position, object);*/
}
// Get the count
@Override
public int getCount() {
int size = 0;
if (productDetails != null) {
imagelists = productDetails.getImagelist();
if (imagelists != null) {
size = imagelists.size();
}
}
Log.d(TAG,"Adapter Size = "+size);
return size;
}
// Write as it is. I don't know much about it
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
Run Code Online (Sandbox Code Playgroud)
}
希望这有用!!
我有同样的问题,但我的问题是在方向改变时发生的.其他解决方案都没有奏效.所以事实证明setRetainInstance(true);,当我根据屏幕尺寸进行两个或一个窗格布局时,我忘记从我的片段中删除.
以防万一有人犯了和我一样的愚蠢错误;检查您没有在某处覆盖活动内容(即寻找对 setContentView 的其他调用)
在我的例子中,由于不小心复制和粘贴,我在片段中使用了 DataBindingUtil.setContentView,而不是 DataBindingUtil.inflate,这弄乱了 Activity 的状态。
使用嵌套片段
通过使用getChildFragmentManager()而不是已getActivity.getSupportFragmentManager()解决的崩溃java.lang.IllegalArgumentException:找不到我的id视图.
小智 5
我的错误在于FragamentTransaction.
我这样做t.replace(R.layout.mylayout);而不是t.replace(R.id.mylayout);
不同的是,一个是布局,另一个是参考 layout(id)
setContentView()我在我的项目中使用视图绑定,并且在膨胀ActivityHelloWorldBinding类后不注意添加:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityHelloWorldBinding.inflate(layoutInflater)
// Add this line.
setContentView(binding.root)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
231513 次 |
| 最近记录: |