"SetView debe haber sido llamado" – Custom Clase extendiendo el botón

Así que tengo una rejilla de 12 x 9 de "botones" llamados "azulejos". Tile.java extiende el widget "Botón".

Tengo un problema ahora, sin embargo, donde estoy tratando de obtener el botón para mostrar un brindis que recuerda la ID del botón empujado.

La cuadrícula se agrega dinámicamente y quiero que permanezca de esa manera.

Aquí está el código de GameBoardActivity.java:

Esto hace que la cuadrícula 12×9 de "azulejos" y añade un oyente para cada uno.

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); } 

Y aquí está el método tileClick mencionado anteriormente:

  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(); } } 

El LogCat muestra lo siguiente:

 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) 

¿Que esta pasando? Tengo prácticamente el mismo código para mostrar un brindis en mi MainActivity.java y que se ejecuta sin ningún problema. ¿Es porque he extendido el botón y no sabe lo que la vista ya es? Además, Eclipse no me deja añadir setView () por alguna razón. Estoy seguro de que tengo problemas de encapsulación aquí, pero estoy confundido en cuanto a lo que son. Gracias por su ayuda por adelantado. JRad

Cambiar las líneas de código a continuación, a la que sigue:

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

Cambiar a esto:

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

Como se puede ver en el código fuente, esa excepción se activa sólo cuando mNextView es nulo. La función "makeText" se supone que debe establecer, y lo hace, pero su código original no captura la referencia a la tostada que construye. En su lugar, su código original crea dos tostadas, y los intentos de "mostrar" el que aún no ha tenido su vista establecida.

 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; } 
  • ¿Cómo mostrar Toast de un Servicio después de que la actividad principal termine?
  • ¿Por qué no aparece mi brindis?
  • Java.lang.RuntimeException: No se puede crear el controlador dentro de hilo que no ha llamado Looper.prepare ();
  • ¿Terminar la actividad después de que el mensaje del pan tostado desaparezca?
  • Cómo utilizar Tostadas cuando no puedo usar "esto" como contexto
  • Cómo hacer tostadas de otro hilo (sans runOnUiThread)
  • ¿De dónde viene esta tostada?
  • Tostadas personalizadas para Android
  • ¿Mostrar dos mensajes Toast a la vez?
  • Establezca Android Toast duración a ser realmente largo (por ejemplo, 1 minuto)
  • Cómo hacer que el mensaje de tostadas personalizado tome toda la pantalla
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.