ViewStub 空指针异常
我的视图存根有问题。
这是我的java文件中的ViewStub
ViewStub stub = (ViewStub) findViewById(R.id.stub1);
ArrayAdapter<String> content=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,files);
View inflated=stub.inflate();
ListView people=(ListView)inflated.findViewById(R.id.friends);
people.setAdapter(content);
这是我初始化Viewstub的xml,
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id = "@+id/linear_details" >
<include layout="@layout/home_back_next_bar" />
<ViewStub android:id="@+id/stub1" android:inflatedId="@+id/showlayout"
android:layout="@layout/layout1" android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</merge>
我在视图存根中膨胀的布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/friends"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
创建列表视图实例以填充它时,此代码给了我一个NullPointerException。 任何想法导致此问题的原因以及如何解决。
预先非常感谢
Log cat 中的错误:
I have a problem with the view stub.
This is the ViewStub in my java file
ViewStub stub = (ViewStub) findViewById(R.id.stub1);
ArrayAdapter<String> content=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,files);
View inflated=stub.inflate();
ListView people=(ListView)inflated.findViewById(R.id.friends);
people.setAdapter(content);
This is the xml where I initialize the Viewstub
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id = "@+id/linear_details" >
<include layout="@layout/home_back_next_bar" />
<ViewStub android:id="@+id/stub1" android:inflatedId="@+id/showlayout"
android:layout="@layout/layout1" android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</merge>
my layout file inflated in the view stub is as follows:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/friends"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
This code gives me a NullPointerException when creating instance of the listview to fill it.
Any Ideas what causes this problem and how to solve.
Thanks alot in advance
Error in Log cat:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有成功的原因:
根据文档:
http://developer.android.com/reference/android/view/ViewStub .html#inflate()
您正在“膨胀的布局资源”上执行 findViewById,这正是您正在寻找的列表视图。
。
The reason it didnt work:
According to the docs :
http://developer.android.com/reference/android/view/ViewStub.html#inflate()
You are doing a findViewById on "The inflated layout resource" which is the very listview you are looking for.
.
没有使用它,而是使用
我
了它,但它不知道为什么??但它做到了:))
Instead of using
I used
And it worked no idea why??. but it did :))