Java – “setView must have been called” – Custom class extension button

“setView must have been called” – Custom class extension button… here is a solution to the problem.

“setView must have been called” – Custom class extension button

So I have a 12 x 9 “button” grid called a “figure block”. extends the widget “button”.

I’m running into an issue now, though, I’m trying to get the button to display a toast that remembers the ID of the button pressed.

The grid is added dynamically and I want it to stay that way.

This is the code from

This generates a 12×9 “square block” grid and adds a listener for each party block.

    public void gridRowButtons(int iterations){
    final Tile[] gridSpaces = new Tile[12];
    LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
    for (int i = 0; i < 12; i++) {
        String idString = String.valueOf(aToI[iterations]) + String.valueOf(i + 1);
        final int id = getResources().getIdentifier(idString, "id", getPackageName());
        gridSpaces[i] = new Tile(getApplicationContext());
        OnClickListener showID = new OnClickListener(){
            public void onClick(View view){                 
                TextView text = (TextView) findViewById(;
                String tileID = getApplicationContext().getResources().getResourceEntryName(id);
                Tile clickedTile = (Tile) findViewById(id);
                clickedTile.tileClick(0, tileID);
    LinearLayout buttonRow = new LinearLayout(getApplicationContext());
    LinearLayout boardSpace = (LinearLayout) this.findViewById(;
    for (int i = 0; i < gridSpaces.length; i++) {

Here is the tileClick method mentioned above:

    public void tileClick(int action, String tileID) {
    switch(action) {
        case 1 :
            action 1
        case 2 :
            action 2
        default :
            Context context = getContext();
            Toast toast = new Toast(context);
            Toast.makeText(context, tileID, Toast.LENGTH_SHORT);

LogCat displays the following:

02-22 20:45:14.623: E/AndroidRuntime(7868): FATAL EXCEPTION: main
02-22 20:45:14.623: E/AndroidRuntime(7868): java.lang.RuntimeException: setView must have been called
02-22 20:45:14.623: E/AndroidRuntime(7868):     at
02-22 20:45:14.623: E/AndroidRuntime(7868):     at com.jneal.ecquire.Tile.tileClick(
02-22 20:45:14.623: E/AndroidRuntime(7868):     at com.jneal.ecquire.GameBoardActivity$1.onClick(
02-22 20:45:14.623: E/AndroidRuntime(7868):     at android.view.View.performClick(
02-22 20:45:14.623: E/AndroidRuntime(7868):     at android.view.View$
02-22 20:45:14.623: E/AndroidRuntime(7868):     at android.os.Handler.handleCallback(
02-22 20:45:14.623: E/AndroidRuntime(7868):     at android.os.Handler.dispatchMessage(
02-22 20:45:14.623: E/AndroidRuntime(7868):     at android.os.Looper.loop(
02-22 20:45:14.623: E/AndroidRuntime(7868):     at
02-22 20:45:14.623: E/AndroidRuntime(7868):     at java.lang.reflect.Method.invokeNative(Native Method)
02-22 20:45:14.623: E/AndroidRuntime(7868):     at java.lang.reflect.Method.invoke(
02-22 20:45:14.623: E/AndroidRuntime(7868):     at$
02-22 20:45:14.623: E/AndroidRuntime(7868):     at
02-22 20:45:14.623: E/AndroidRuntime(7868):     at dalvik.system.NativeStart.main(Native Method)

What’s going on? I got almost identical code for displaying toasts in my and it executed without problems.
Is it because I extended the Button and it doesn’t know what the View already is?
Also, Eclipse didn’t allow me to add setView() for some reason. I’m sure I’m having encapsulation issues here, but I’m confused about what they are.
Thank you in advance for your help.


Change the following

line of code to the following line of code:

        Toast toast = new Toast(context);
        Toast.makeText(context, tileID, Toast.LENGTH_SHORT);;

Change to:

    Toast toast = Toast.makeText(context, tileID, Toast.LENGTH_SHORT);;       

As you can see from the source code, the exception is thrown only if mNextView is null.
The function “makeText” should set it, it does set it, but your original code doesn’t capture a reference to the toast it built. Instead, your original code creates two toasts and tries to “show” the one whose View has not been set.

public void show() {
    if (mNextView == null) {
        throw new RuntimeException("setView must have been called");


public static Toast makeText(Context context, CharSequence text, int duration) {
    Toast result = new Toast(context);

LayoutInflater inflate = (LayoutInflater)
    View v = inflate.inflate(, null);
    TextView tv = (TextView)v.findViewById(;

result.mNextView = v;
    result.mDuration = duration;

return result;

Related Problems and Solutions