gpt4 book ai didi

android - 在 ViewStub 中使用 ListView 时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 02:05:48 25 4
gpt4 key购买 nike

我正在尝试在 ViewStub 中创建一个 ListView,但我一直收到 NullPointerException。我的按钮在 ViewStub 中按预期工作。失败的代码如下:

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

我认为它在 commentsList.setAdapter(commentsAdapter) 处失败了。

这个包含 ListView 的 ViewStub 内容的 XML 文件如下:

<?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>

我已经在不使用 viewstub 的情况下测试了 ListView 的代码,它工作得很好。

我想我明白了吗?任何帮助表示赞赏! :)

编辑:

感谢快速回复!

contentView 设置在 ViewStub 膨胀的 View 上。但正如所提到的,它与按钮一起工作得很好,但我不明白为什么我用 ListView 得到一个空指针! :(

这是 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

改变它,它应该可以工作。

关于android - 在 ViewStub 中使用 ListView 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10106921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com