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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:59:59+00:00 2026-05-19T02:59:59+00:00

I have a simple test application with a GridView containing an array of buttons,

  • 0

I have a simple test application with a GridView containing an array of buttons, I’m trying to perform an operation (any operation) touching one of these buttons.

The main code is this:

package com.example.convert;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;

public class convert extends Activity {
    //private ListView List;

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

        String [] elenco = {
                "ciao","questo","è","un esempio","Configurazione"
                };

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.oggetto,R.id.testogg,elenco);
        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(arrayAdapter);
        Button btn = (Button) (findViewById(R.id.testogg));


        btn.setOnClickListener(new OnClickListener(){
              @Override
              public void onClick(View v) {
                  setContentView(R.layout.oggetto);
              }

            });

    }

}

The main.xml is the following:

<?xml version="1.0" encoding="utf-8"?>

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

And oggetto.xml contains the following lines:

<?xml version="1.0" encoding="utf-8"?>

<Button
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:text="@+id/TextView01"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:id="@+id/testogg">
</Button>

But when I try to run the application it return me a NullPointerException on the btn.setOnClickListener(new OnClickListener()) line.

May be that happen because the buttons have the same id? If this is the case, how can I obtain repeated buttons with different ids?

Thanks for your help!

  • 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-05-19T02:59:59+00:00Added an answer on May 19, 2026 at 2:59 am

    May be that happen cause the buttons
    have the same id

    That could be. But it also shows something is wrong if that’s even the case. Each button specified in XML is only created once. You can’t create multiple buttons just by calling findViewById() multiple times or by passing in the resource ID.

    But the problem comes because you call setContentView(R.layout.main);

    findViewById() will ONLY return non-null views for views contained in main.xml. Your context knows nothing about your other XML layouts because they’re not inflated.

    You should probably start with simpler programs and go through the tutorials to get a feeling for how android views work.

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

Sidebar

Related Questions

I have written the following simple test in trying to learn Castle Windsor's Fluent
I have a simple question related to one-line programming. First an example: function test(a)
I have a web service project and simple test application. I can debug the
I have a simple test page in my Silverlight 4 application in which I'm
I have a simple little test app written in Flex 3 (MXML and some
I have an extremely simple unit test. I'm not sure why it fails when
I have recorded a very simple test case Using the Selenium IDE integrated with
I have a very simple question. I want to test whether a particular port
I was trying to export a simple test function for a dll to work
Very simply put, I have the following code snippet: FILE* test = fopen(C:\\core.u, w);

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.