java - 从另一个 xml 布局添加 ListView 时出现 NullPointerException

标签 java android listview nullpointerexception illegalstateexception

feedLayout = new LinearLayout(this);
feedLayout.setOrientation(LinearLayout.VERTICAL);
//LayoutInflater inflater = (LayoutInflater)  getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = getLayoutInflater().inflate(R.layout.list_feeds, feedLayout, false);
lstFeeds = (ListView) v.findViewById(R.id.listfeeds);
lstFeeds.setAdapter(new FeedsAdapter(this, android.R.layout.simple_list_item_1, lista));
feedLayout.addView(lstFeeds);

日志:

IllegalStateException:
The specified child already has a parent you must call removeChild() ...

最佳答案

你需要使用

feedLayout.removeAllViews();

之前

feedLayout.addView(v);

编辑:

feedLayout = new LinearLayout(this);
feedLayout.setOrientation(LinearLayout.VERTICAL);
//LayoutInflater inflater = (LayoutInflater)  getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = getLayoutInflater().inflate(R.layout.list_feeds, feedLayout, false);
lstFeeds = (ListView) v.findViewById(R.id.listfeeds);
lstFeeds.setAdapter(new FeedsAdapter(this, android.R.layout.simple_list_item_1, lista));
feedLayout.addView(v);

关于java - 从另一个 xml 布局添加 ListView 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18249095/

相关文章:

java - 如何将 Wikia 链接放入 Android 中的 ListView 中,也许 MediaWiki API 会有所帮助,但我不确定该去哪里

android - 如何监听 ListView 上发生在列表项之外的点击事件?

java - 寻找 AngularJS 客户端的 App Engine Channel Api Java 替代解决方案

java - Firebase 实时数据库不在 Recyclerview 中显示图像

java - 应用程序无法打开,LogCat 中不断出现以下错误

java - WindowManager removeView 抛出 "Hardware acceleration ..."异常

java - 从.NET客户端访问axis2服务时的Web服务参数类型

java - 下面的代码中 asynctask 是否正确使用?

android - 在 React Native 中的两个独立应用程序之间共享代码

android - 单击列表项时如何将单选按钮设置为选中