在使用NullPointerExeption一个ViewStub内的ListView [英] NullPointerExeption when using ListView inside a ViewStub
问题描述
我试图创造一个ViewStub里面一个ListView,但我一直在得到一个NullPointerExeption。我的按钮按预期工作ViewStub内。在code失败是如下:
I'm trying to create a ListView inside a ViewStub, but i keep on getting a NullPointerExeption. My Buttons works as intended inside the ViewStub. The code that fails is as following:
else if(v.getId() == R.id.comments_button_series_view){
if(stubUsed == false){
stub = (ViewStub) findViewById(R.id.stub);
View inflatedStub = stub.inflate();
String[] comments = new String[]{"comment1", "comment2"};
commentsAdapter = new ArrayAdapter<String>(this,
R.layout.patient_list_element,
R.id.comments_list_comments_view, comments);
commentsList = (ListView)findViewById(R.id.comments_list_comments_view);
commentsList.setAdapter(commentsAdapter);
我觉得failes在commentsList.setAdapter(commentsAdapter)。
I think it failes at commentsList.setAdapter(commentsAdapter).
与持有的ListView中ViewStub的内容该XML文件是如下:
This XML file with the content of the ViewStub that holds the ListView is as following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/comment_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/settings_background" >
<Button
android:id="@+id/comments_button_comment_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="62dp"
android:text="Add comment" />
<ListView
android:id="@+id/comments_list_comments_view"
android:layout_width="match_parent"
android:layout_height="470dp"
android:layout_alignLeft="@+id/pin_button_comments_view"
android:layout_below="@+id/comments_button_comment_view" >
</ListView>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/comments_button_comment_view"
android:layout_marginRight="119dp"
android:text="COMMENTS"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/pin_button_comments_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/comments_button_comment_view"
android:layout_alignBottom="@+id/comments_button_comment_view"
android:layout_marginRight="30dp"
android:layout_toLeftOf="@+id/comments_button_comment_view"
android:text="Add Pin" />
<Button
android:id="@+id/hide_button_comments_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/pin_button_comments_view"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:text="Hide" />
</RelativeLayout>
我已经测试code为ListView不使用viewstub,而且工作得很好。
I have tested the code for the ListView without using a viewstub, and that works just fine.
我觉得我得到了这一切?任何帮助AP preciated! :)
I think i got it all? Any help is appreciated! :)
编辑:
感谢您的回复快!
在内容查看设置在那里的ViewStub膨胀的看法。但是如前所述,它的工作原理只是按键很好,但我不知道为什么我得到ListView的一个空指针! (
THe contentView is set on the view where the ViewStub is inflated. But as mentioned, it works just fine with buttons, but i don't get why i get a nullpointer with the ListView! :(
下面是从LogCat中的结果:
Here is the outcome from the LogCat:
04-11 10:37:55.400: W/dalvikvm(971): threadid=1: thread exiting with uncaught exception (group=0x40014760)
04-11 10:37:55.450: E/AndroidRuntime(971): FATAL EXCEPTION: main
04-11 10:37:55.450: E/AndroidRuntime(971): java.lang.NullPointerException
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:396)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:366)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.AbsListView.obtainView(AbsListView.java:1964)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.ListView.makeAndAddView(ListView.java:1756)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.ListView.fillDown(ListView.java:656)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.ListView.fillFromTop(ListView.java:716)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.ListView.layoutChildren(ListView.java:1609)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.AbsListView.onLayout(AbsListView.java:1794)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.View.layout(View.java:9330)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.ViewGroup.layout(ViewGroup.java:3795)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.View.layout(View.java:9330)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.ViewGroup.layout(ViewGroup.java:3795)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.View.layout(View.java:9330)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.ViewGroup.layout(ViewGroup.java:3795)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.FrameLayout.onLayout(FrameLayout.java:400)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.View.layout(View.java:9330)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.ViewGroup.layout(ViewGroup.java:3795)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1542)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1403)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.LinearLayout.onLayout(LinearLayout.java:1314)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.View.layout(View.java:9330)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.ViewGroup.layout(ViewGroup.java:3795)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.FrameLayout.onLayout(FrameLayout.java:400)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.View.layout(View.java:9330)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.ViewGroup.layout(ViewGroup.java:3795)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.ViewRoot.performTraversals(ViewRoot.java:1201)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.ViewRoot.handleMessage(ViewRoot.java:1944)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.os.Looper.loop(Looper.java:126)
04-11 10:37:55.450: E/AndroidRuntime(971): at android.app.ActivityThread.main(ActivityThread.java:3997)
04-11 10:37:55.450: E/AndroidRuntime(971): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 10:37:55.450: E/AndroidRuntime(971): at java.lang.reflect.Method.invoke(Method.java:491)
04-11 10:37:55.450: E/AndroidRuntime(971): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
04-11 10:37:55.450: E/AndroidRuntime(971): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
04-11 10:37:55.450: E/AndroidRuntime(971): at dalvik.system.NativeStart.main(Native Method)
再次感谢:)
推荐答案
有似乎当你创建 ArrayAdapter
对象是一个错误。
在构造函数中的第三个参数( textViewResourceId
)应该是一个的TextView
代替的ListView
。
改变这种状况,它应该是工作。
There seems to be an error when you are creating the ArrayAdapter
object.
The third parameter in the constructor (textViewResourceId
) should be a TextView
instead of a ListView
.
Change that and it should be working.
这篇关于在使用NullPointerExeption一个ViewStub内的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!