Java 从其他xml布局添加ListView时出现NullPointerException
Logcat:Java 从其他xml布局添加ListView时出现NullPointerException,java,android,listview,nullpointerexception,illegalstateexception,Java,Android,Listview,Nullpointerexception,Illegalstateexception,Logcat: 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 = 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);
更好的方法是使用自定义适配器扩展基本适配器,并在getView()方法中扩展布局 logcat已经告诉您必须做什么:-)这是怎么回事<代码>馈线布局=新的线性布局(本)代码>创建一个空的
线性布局
。为什么需要调用feedLayout.removeAllViews()
呢?@vikram需要先从父视图中删除视图。Hardy,thanx这解决了问题,很抱歉删除了我以前的视图comment@SomePenguin没关系。。快乐编码。。。很高兴帮助你,我明白了。你有feedLayout.addView(lstFeeds)代码>在编辑答案之前,在这种情况下,删除视图不会有帮助。例外情况也会是一样的。即使现在,@SomePenguin仍然可以删除feedLayout.removeallview()代码>来自他/她的代码,没有问题。
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);