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!

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Great answer!!! this gave me the hint that i've looking for! thanks! but if so... how cai i refer to a view that is loaded from a different XML?
You can't. The View doesn't exist until it's been inflated, which is what happens when you set your content view to main.xml. The XML documents are more or less blueprints telling Android how to construct the Views. The layout inflater does the actual building.
An alternative is to just create a new Button from code. Adding that button to an already inflated view is not trivial though (I'm not even sure if it's possible). If you need a dynamic layout, you need to create your views from code.
1

What are you trying to accomplish? You're originally setting a layout that only contains a GridView, then you're trying to find a Button that doesn't even exist (at the faulting line, your content view is your main.xml document, which does not contain any buttons, let alone one with the id of testogg). This means that btn is null, as the button does not exist. That is the cause of your NullPointerException.

If you want to intercept a click event for an item in your GridView, you need to be using the OnItemClickListener, and attaching that to your GridView. See the link for the onItemClick method definition.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.