"setView must have been called" - Custom Class extending button

JRad the Bad

So I've got a 12 x 9 grid of "buttons" called "tiles". Tile.java extends the widget "Button".

I've got an issue right now though, where I'm trying to get the button to display a toast that recalls the ID of the button pushed.

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

Here's the code from GameBoardActivity.java:

This makes the 12x9 grid of "tiles" and adds a listener for each.

    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());
        gridSpaces[i].setText("");
        gridSpaces[i].setHeight(40);
        gridSpaces[i].setWidth(40);
        gridSpaces[i].setLayoutParams(buttonParams);
        gridSpaces[i].setId(id);
        OnClickListener showID = new OnClickListener(){
            public void onClick(View view){                 
                TextView text = (TextView) findViewById(R.id.tileIDText);
                String tileID = getApplicationContext().getResources().getResourceEntryName(id);
                text.setText(tileID);
                Tile clickedTile = (Tile) findViewById(id);
                clickedTile.tileClick(0, tileID);
            }
        };
        gridSpaces[i].setOnClickListener(showID);
    }
    LinearLayout buttonRow = new LinearLayout(getApplicationContext());
    buttonRow.setOrientation(LinearLayout.HORIZONTAL);
    buttonRow.setLayoutParams(buttonParams);
    LinearLayout boardSpace = (LinearLayout) this.findViewById(R.id.boardLayout);
    for (int i = 0; i < gridSpaces.length; i++) {
        buttonRow.addView(gridSpaces[i]);
    }
    boardSpace.addView(buttonRow);
}

And here's 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);
            toast.show();
    }
}

The LogCat shows 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 android.widget.Toast.show(Toast.java:103)
02-22 20:45:14.623: E/AndroidRuntime(7868):     at com.jneal.ecquire.Tile.tileClick(Tile.java:51)
02-22 20:45:14.623: E/AndroidRuntime(7868):     at com.jneal.ecquire.GameBoardActivity$1.onClick(GameBoardActivity.java:55)
02-22 20:45:14.623: E/AndroidRuntime(7868):     at android.view.View.performClick(View.java:3627)
02-22 20:45:14.623: E/AndroidRuntime(7868):     at android.view.View$PerformClick.run(View.java:14329)
02-22 20:45:14.623: E/AndroidRuntime(7868):     at android.os.Handler.handleCallback(Handler.java:605)
02-22 20:45:14.623: E/AndroidRuntime(7868):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-22 20:45:14.623: E/AndroidRuntime(7868):     at android.os.Looper.loop(Looper.java:137)
02-22 20:45:14.623: E/AndroidRuntime(7868):     at android.app.ActivityThread.main(ActivityThread.java:4511)
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(Method.java:511)
02-22 20:45:14.623: E/AndroidRuntime(7868):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
02-22 20:45:14.623: E/AndroidRuntime(7868):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
02-22 20:45:14.623: E/AndroidRuntime(7868):     at dalvik.system.NativeStart.main(Native Method)

What's going on? I've got virtually the exact same code for displaying a toast in my MainActivity.java and that executes with no problem. Is it because I've extended Button and it doesn't know what the view is already? Also, Eclipse won't let me add setView() for some reason. I'm sure I've got encapsulation issues here but I'm confused as to what they are. Thanks for your help in advance. JRad

NameSpace

Change the lines of code below, to the one's that follow:

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

Change to this:

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

As you can see from the source code, that exception is thrown only when mNextView is null. The function "makeText" is suppose to set it, and it does, but your original code does not capture the reference to the Toast it builds. Instead, your original code creates two Toasts, and attempts to "show" the one which has not yet had its view 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)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
    TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
    tv.setText(text);

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

    return result;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

While showing a toast in android code, I get the following error: java.lang.RuntimeException: setView must have been called

From Dev

Create custom button with Swift by extending the UIButton class

From Dev

Extending Button class

From Dev

Extending QFrame in custom class

From Dev

Extending a custom resumable download class

From Dev

Extending Button to make a reusable custom button

From Dev

Extending Button to make a reusable custom button

From Dev

Extending a custom layout class - Constructor never used

From Dev

ArrayList cannot be cast to custom class extending ArrayList

From Dev

Extending Parse.User to a custom user class

From Dev

Extending a class

From Dev

Extending Button.class: Drawable and Text change color onTouchEvent()

From Dev

Custom security is not working when extending the WebSecurityConfigurerAdapter class in different package

From Dev

Add custom class to bootstrap button

From Dev

ActionListener with a custom Button class (Java)

From Dev

Error inflating custom button class

From Dev

unbindService inside custom button class

From Dev

Add custom class to bootstrap button

From Dev

Apply default Button style to custom Button class

From Dev

Extending vs not extending a class in PHP

From Dev

Android - Is it OK to use a custom class for static members and methods instead of extending an Application class

From Dev

Extending Custom Behavior

From Dev

Extending jshint with custom checks

From Dev

Extending NSMutableURLRequest with custom fields

From Dev

Custom HashMap by extending

From Dev

Extending Custom Behavior

From Dev

Error inflating custom button class. NoSuchMethodException

From Dev

Is it possible to add a class to a custom tinyMCE button?

From Dev

Angular Material Custom Class Button Issue

Related Related

  1. 1

    While showing a toast in android code, I get the following error: java.lang.RuntimeException: setView must have been called

  2. 2

    Create custom button with Swift by extending the UIButton class

  3. 3

    Extending Button class

  4. 4

    Extending QFrame in custom class

  5. 5

    Extending a custom resumable download class

  6. 6

    Extending Button to make a reusable custom button

  7. 7

    Extending Button to make a reusable custom button

  8. 8

    Extending a custom layout class - Constructor never used

  9. 9

    ArrayList cannot be cast to custom class extending ArrayList

  10. 10

    Extending Parse.User to a custom user class

  11. 11

    Extending a class

  12. 12

    Extending Button.class: Drawable and Text change color onTouchEvent()

  13. 13

    Custom security is not working when extending the WebSecurityConfigurerAdapter class in different package

  14. 14

    Add custom class to bootstrap button

  15. 15

    ActionListener with a custom Button class (Java)

  16. 16

    Error inflating custom button class

  17. 17

    unbindService inside custom button class

  18. 18

    Add custom class to bootstrap button

  19. 19

    Apply default Button style to custom Button class

  20. 20

    Extending vs not extending a class in PHP

  21. 21

    Android - Is it OK to use a custom class for static members and methods instead of extending an Application class

  22. 22

    Extending Custom Behavior

  23. 23

    Extending jshint with custom checks

  24. 24

    Extending NSMutableURLRequest with custom fields

  25. 25

    Custom HashMap by extending

  26. 26

    Extending Custom Behavior

  27. 27

    Error inflating custom button class. NoSuchMethodException

  28. 28

    Is it possible to add a class to a custom tinyMCE button?

  29. 29

    Angular Material Custom Class Button Issue

HotTag

Archive