Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从其他xml布局添加ListView时出现NullPointerException_Java_Android_Listview_Nullpointerexception_Illegalstateexception - Fatal编程技术网

Java 从其他xml布局添加ListView时出现NullPointerException

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,

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, 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);