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

  • Home
  • SEARCH
  • 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 8143523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:08:35+00:00 2026-06-06T13:08:35+00:00

I have a ListView with a custom layout for the rows (list items) and

  • 0

I have a ListView with a custom layout for the rows (list items) and the list is being populated with an adapter as usual. In this custom layout, I’ve added an ImageView and TextView (so there’s an icon to match the text). I’m unable to get the ImageView based on the position in the ListView (Need to do this so I can set the icon to match the text obviously)and the only real information I get from the error log is the line the error is on but I’ll post it anyways. Any help is greatly appreciated. I really don’t want to create a mess by having to remove the ListView, create a bunch of RelativeLayouts, encompass them in a LinearLayout and put that inside a Scrollview.

Relevant Code:-

ListView listView = (ListView) findViewById(R.id.servicelist);
String[] values = new String[] { "Choice0", "Choice1", "Choice2",
        "Choice3", "Choice4", "Choice5" };

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.services_row,
android.R.id.text1, values);

listView.setAdapter(adapter);

for(int position=0; position<11; position++){
    ViewGroup rowView = (ViewGroup) listView.getChildAt(position);
    ImageView icon = (ImageView) rowView.findViewById(R.id.service_row_icon); //ERROR HERE
    switch(position){
    case 0:{ icon.setImageResource(R.drawable.ic_launcher); break; }
    case 1:{ icon.setImageResource(R.drawable.ic_launcher); break; }
    //ETC ETC More cases go here, irrelevant code
    }
}

Row layout (service_row.xml):-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageView
    android:id="@+id/service_row_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"/>

<TextView
    android:id="@+id/service_row_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#33b5e5"
    android:paddingLeft="5dp"/>

</LinearLayout>

Error log:-

06-28 01:46:31.581: E/AndroidRuntime(18070): FATAL EXCEPTION: main
06-28 01:46:31.581: E/AndroidRuntime(18070): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dragonheart.autodroid/com.dragonheart.autodroid.ServiceClasses.AddServiceActivity}: java.lang.NullPointerException
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.os.Looper.loop(Looper.java:137)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.app.ActivityThread.main(ActivityThread.java:4507)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at java.lang.reflect.Method.invokeNative(Native Method)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at java.lang.reflect.Method.invoke(Method.java:511)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at dalvik.system.NativeStart.main(Native Method)
06-28 01:46:31.581: E/AndroidRuntime(18070): Caused by: java.lang.NullPointerException
06-28 01:46:31.581: E/AndroidRuntime(18070):    at com.dragonheart.autodroid.ServiceClasses.AddServiceActivity.onCreate(AddServiceActivity.java:43)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.app.Activity.performCreate(Activity.java:4465)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
06-28 01:46:31.581: E/AndroidRuntime(18070):    ... 11 more
  • 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-06T13:08:36+00:00Added an answer on June 6, 2026 at 1:08 pm

    You need to do that inside your getView of your adapter, not outside of it. It crashes because the framework doesn’t have a clue which of the many views in your list that you want.

    Inside the adapter, it will know the position and be able to return the proper view.

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

Sidebar

Related Questions

I have a listview connected to a custom array adapter. This list shows information
I have a ListView which contains custom rows. This custom row has following UI
I have a custom ListView Adapter , with which I'm creating rows for a
I have a ListView with a custom list adapter (that extends BaseAdapter). My list
I have following situation: I have a custom ListView with ImageView and TextView in
I have a ListView with a custom Adapter that creates rows containing TextViews that
I have a ListView which uses a custom adapter to display rows of data.
I have a listview with a custom adapter. Each listview item has an ImageView.
I have a listView with custom objects defined by the xml-layout below. I want
I have a ListView with custom Adapter . To be honest, I have many

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.