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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:52:25+00:00 2026-06-09T20:52:25+00:00

Writing an application Menu.java extending ListActivity and I have added a TextView header at

  • 0

Writing an application Menu.java extending ListActivity and I have added a TextView header at the top above my list activity.

The layout of header and list activity are exactly what I want, but when I click on say Item1 the toast pops up and says “Item2” selected. When I click on Item2… “Item3” Selected. But when I click on Item3, the app crashes and displays a bunch of Runtime Errors.

It did not do this before I had the header implemented. I have searched and searched stackoverflow and the android dev site and can not seem to find the answer.

I have already made the header set to false so that it is not clickable.

Thanks in advance! Sorry if I have missed something obvious… newbie here. 😛

//this is Menu.java


package com.example.mytexts;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Menu extends ListActivity {

String[] values = new String[] { "Item1", "Item2", "Item3" };

public void onCreate(Bundle biscuits) {
    super.onCreate(biscuits);

    setContentView(R.layout.text_layout);

    ListView lv = getListView();
    LayoutInflater inflater = getLayoutInflater();
    View header = inflater.inflate(R.layout.header,
            (ViewGroup) findViewById(R.id.header__root));
    lv.addHeaderView(header, null, false);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
}

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    String cheese = (String) getListAdapter().getItem(position);
    Toast.makeText(this, cheese + " selected", Toast.LENGTH_SHORT).show();

    try {
        Class ourClass = Class.forName("com.example.addsubtract." + cheese);
        Intent ourIntent = new Intent(Menu.this, ourClass);
        startActivity(ourIntent);

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
  }
}


//this is header.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/header__root"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:orientation="vertical" >

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/darkblue"
    android:gravity="center"
    android:padding="10dp"
    android:text="Messages"
    android:textSize="20dp" >
</TextView>

</LinearLayout>


//this is text_layout.xml


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

<ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none" >
</ListView>

<TextView
    android:id="@+id/android:empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Nothing to Display" >
</TextView>

</LinearLayout>
  • 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-09T20:52:26+00:00Added an answer on June 9, 2026 at 8:52 pm

    The position (giving by onListItemClick) is tied up with the number of items in the ListView including header (and footer) and not with the adaptor.

    Instead of:

    String cheese = (String) getListAdapter().getItem(position);
    

    Use:

    String cheese = (String) l.getItemAtPosition(position);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an android application using eclipse. I have a main activity called MenuActivity
i m writing an application n stuckup with one problem.I have three activity A,B
I'm writing a Cocoa application that installs itself as an menulet in the menu
I am writing an application that will allow an android phone and java application
I'm writing a Java Swing application for the Mac using Java 1.6. I've read
On this winform application I am writing, I want to secure one menu item
I am writing an WPF application with a menu. it needs 2 to 4
I am writing a C# application and it takes files as argument, I added
I am writing a helper for my application which writes out a menu item
I have a Java Swing application, that has many JTextFields and a datamodel. When

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.