Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7830933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:28:56+00:00 2026-06-02T11:28:56+00:00

I’m trying to create a ListView inside a ViewStub, but i keep on getting

  • 0

I’m trying to create a ListView inside a ViewStub, but i keep on getting a NullPointerException. 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);

I think it failes at commentsList.setAdapter(commentsAdapter).

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>

I have tested the code for the ListView without using a viewstub, and that works just fine.

I think i got it all? Any help is appreciated! 🙂

EDIT:

Thanks for fast reply!

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! 🙁

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)

Thanks again:)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:28:57+00:00Added an answer on June 2, 2026 at 11:28 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.