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 8272913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:17:34+00:00 2026-06-08T07:17:34+00:00

i am creating a listView using an ArrayAdapter. Each View contain 4 TextViews and

  • 0

i am creating a listView using an ArrayAdapter. Each View contain 4 TextViews and an ImageButton. When i click on a row it displays data in a table, and if i click on the imageButton which is in the row it sends the user to another activity.

My problem is : When i scroll the list and i click on an imageButton (which was not in the visible part of the list at the beggining) i have an Exception

07-23 11:07:50.150: E/AndroidRuntime(9326): FATAL EXCEPTION: main
07-23 11:07:50.150: E/AndroidRuntime(9326): java.lang.IllegalStateException: Could not execute method of the activity
07-23 11:07:50.150: E/AndroidRuntime(9326):     atandroid.view.View$1.onClick(View.java:2700)
07-23 11:07:50.150: E/AndroidRuntime(9326):at ndroid.view.View.performClick(View.java:3127)
07-23 11:07:50.150: E/AndroidRuntime(9326): at android.view.View$PerformClick.run(View.java:12025)
07-23 11:07:50.150: E/AndroidRuntime(9326):     at android.os.Handler.handleCallback(Handler.java:587)
07-23 11:07:50.150: E/AndroidRuntime(9326):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-23 11:07:50.150: E/AndroidRuntime(9326):     at android.os.Looper.loop(Looper.java:132)
07-23 11:07:50.150: E/AndroidRuntime(9326):     at android.app.ActivityThread.main(ActivityThread.java:4126)
07-23 11:07:50.150: E/AndroidRuntime(9326):     at java.lang.reflect.Method.invokeNative(Native Method)
07-23 11:07:50.150: E/AndroidRuntime(9326):     at java.lang.reflect.Method.invoke(Method.java:491)
07-23 11:07:50.150: E/AndroidRuntime(9326):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
07-23 11:07:50.150: E/AndroidRuntime(9326):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
07-23 11:07:50.150: E/AndroidRuntime(9326):     at dalvik.system.NativeStart.main(Native Method)
07-23 11:07:50.150: E/AndroidRuntime(9326): Caused by: java.lang.reflect.InvocationTargetException
07-23 11:07:50.150: E/AndroidRuntime(9326):     at java.lang.reflect.Method.invokeNative(Native Method)
07-23 11:07:50.150: E/AndroidRuntime(9326):     at java.lang.reflect.Method.invoke(Method.java:491)
07-23 11:07:50.150: E/AndroidRuntime(9326):     at android.view.View$1.onClick(View.java:2695)
07-23 11:07:50.150: E/AndroidRuntime(9326):     ... 11 more
07-23 11:07:50.150: E/AndroidRuntime(9326): Caused by: java.lang.NullPointerException
07-23 11:07:50.150: E/AndroidRuntime(9326):     at com.android.androidlistview.MainActivity.MyHandler(MainActivity.java:48)

Here is my code :

MainActivity :

public class MainActivity extends ListActivity {

private ListView listView;

private String id;
private static final int REQUEST_CODE = 1;
private AccountNameListAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayList<Account> accountData = new ArrayList<Account>();
    for (int i = 0; i < 50; i++) {
        Account account = new Account("n" + i, "test", "0154654654",
                "gdfg", "gfdg", "2635454564");
        accountData.add(account);
    }

    adapter = new AccountNameListAdapter(this, R.layout.list_detail,
            accountData);

    listView = this.getListView();
    listView.setAdapter(adapter);

}

public void MyHandler(View v) {
    ImageButton cb = (ImageButton) v;

    int position = Integer.parseInt(cb.getTag().toString());


    View o = listView.getChildAt(position).findViewById(R.id.blocCheck);

    TextView t = (TextView) listView.getChildAt(position).findViewById(
            R.id.id);
    Toast.makeText(getApplicationContext(), t.getText(), Toast.LENGTH_LONG)
            .show();

    Intent editIntent = new Intent(MainActivity.this, EditActivity.class);
    editIntent.putExtra("accountId", t.getText());
    startActivityForResult(editIntent, REQUEST_CODE);

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {

        final Account account = new Account("424242", "RhfghOCK", "0154654654",
                "ghfgh", "hgfh", "2635454564");

        adapter.add(account);
        adapter.notifyDataSetChanged();

    }

}

}

line 48 corresponds to :

View o = listView.getChildAt(position).findViewById(R.id.blocCheck);

My custom adater :

public class AccountNameListAdapter extends ArrayAdapter<Account> {


    private Context context;
    private int layoutResourceId;   
    private ArrayList<Account> data;
    private TableLayout table;
    private TextView tableName;
    private TextView tableId;



    public AccountNameListAdapter(Context context, int layoutResourceId, ArrayList<Account> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
        table = (TableLayout) ((Activity) context).findViewById(R.id.tableLayout);
        tableName = (TextView) ((Activity) context).findViewById(R.id.NameText);
        tableId = (TextView) ((Activity) context).findViewById(R.id.idText);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {           

        AccountHolder holder = null;

        if(convertView == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            convertView = inflater.inflate(layoutResourceId, parent, false);

            holder = new AccountHolder();

            convertView.setClickable(true);
            convertView.setFocusable(true);

            convertView.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    new AlertDialog.Builder(context).setTitle("touched").show();                   
                    table.setVisibility(0);
                    tableName.setText("ok name");
                    tableId.setText("ok "+ data.get(position).getAccountId());                      
                }

            });


            holder.txtName = (TextView)convertView.findViewById(R.id.nom);
            holder.txtId = (TextView)convertView.findViewById(R.id.id);

            convertView.setTag(holder);


        }
                   else{
        holder = (AccountHolder)convertView.getTag();
        }

        holder.txtName.setText(data.get(position).getName());
        holder.txtId.setText(data.get(position).getAccountId());


        ImageButton img = (ImageButton) convertView.findViewById (R.id.check);  
        img.setTag (position);
        });


        return convertView;
    }

    static class AccountHolder
    {
        TextView txtName;
        TextView txtId;
    }


}

list_detail.xml :

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


<TableLayout android:id="@+id/blocCheck" 
    android:background="#EEF9F8"
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp">


    <TableRow android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:paddingLeft="4sp"
        android:paddingBottom="4sp">
        <LinearLayout
            android:orientation="horizontal" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:paddingLeft="20sp"
            android:layout_weight="1" 
            android:gravity="center"
            >

            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:textSize="20sp"
                android:text="Name : " 
                android:layout_gravity="left"
                android:layout_marginRight="5sp" />
            <TextView android:id="@+id/nom" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:textSize="20sp"
                android:layout_gravity="left" />
        </LinearLayout>
    </TableRow>


    <TableRow android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:paddingLeft="4sp"
        android:baselineAligned="true">
        <LinearLayout
            android:orientation="horizontal" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:paddingLeft="20sp"
            android:layout_weight="1" 
            android:gravity="center">
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:textSize="20sp"
                android:text="Id : " 
                android:layout_gravity="left"
                android:layout_marginRight="5sp" />
            <TextView android:id="@+id/id" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:textSize="20sp"
                android:layout_gravity="left" />
        </LinearLayout>
        <ImageButton android:layout_height="wrap_content" 
            android:id="@+id/check"
            android:layout_width="wrap_content" 
            android:background="@drawable/edit"
            android:layout_gravity="right"
            android:layout_marginRight="10sp" 
            android:onClick="MyHandler"/>
    </TableRow>
</TableLayout>

  • 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-08T07:17:36+00:00Added an answer on June 8, 2026 at 7:17 am

    The method getChildAt will return, for a ListView, a valid View(null in rest) only if the position corresponds to a visible child on the screen. The integer that is supplied to that method must be in the interval defined by ListView.getLastVisiblePosition() – ListView.getFirstVisiblePosition().

    Try to subtract from the position the value of getFirstVisiblePosition() :

    View o = listView.getChildAt(position - listView.getFirstVisiblePosition()).findViewById(R.id.blocCheck);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a ListView that contains two TextViews and an ImageView. I'm passing data
I am creating a simple RSS reader which displays headlines in ListView, downloading it
I am creating an app using jQuery Mobile. I have a listview from which
Is there any way creating a photo gallery using Listview control ? Im playing
I am using XML file for creating Context Menu for my ListView. (Please see
When creating a website using Asp.Net and using controls such as the ListView is
I am creating a app in android. In that i am using list view.
i am creating a listview with custom arrayadapter..but the problem is in tablet it's
After creating a custom Dialog using a ListView and my own implementation of ListAdapter
I am creating a custom listview. The data is not coming from a cursor

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.