androidtabsandroid-alertdialogactivitygroup

AlertDialog doesnt work inside ActivityGroup


I have a main activity has three tab ADMINISTRADOR. in the first tab called me to another activity within a groupactivity with listview when click on each item has to leave for a AlertDialog I have the following error:

05-25 20:06:58.025: E/AndroidRuntime(26021): FATAL EXCEPTION: main
05-25 20:06:58.025: E/AndroidRuntime(26021): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@44c227a8 is not valid; is your activity running?
05-25 20:06:58.025: E/AndroidRuntime(26021):    at android.view.ViewRoot.setView(ViewRoot.java:505)
05-25 20:06:58.025: E/AndroidRuntime(26021):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
05-25 20:06:58.025: E/AndroidRuntime(26021):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
05-25 20:06:58.025: E/AndroidRuntime(26021):    at android.view.Window$LocalWindowManager.addView(Window.java:424)
05-25 20:06:58.025: E/AndroidRuntime(26021):    at android.app.Dialog.show(Dialog.java:241)
05-25 20:06:58.025: E/AndroidRuntime(26021):    at utp.android.aplicacion.ListarAlumnos$1.run(ListarAlumnos.java:170)
05-25 20:06:58.025: E/AndroidRuntime(26021):    at android.app.Activity.runOnUiThread(Activity.java:3707)
05-25 20:06:58.025: E/AndroidRuntime(26021):    at utp.android.aplicacion.ListarAlumnos.onItemClick(ListarAlumnos.java:149)
05-25 20:06:58.025: E/AndroidRuntime(26021):    at android.widget.AdapterView.performItemClick(AdapterView.java:284)
05-25 20:06:58.025: E/AndroidRuntime(26021):    at android.widget.ListView.performItemClick(ListView.java:3382)
05-25 20:06:58.025: E/AndroidRuntime(26021):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2394)
05-25 20:06:58.025: E/AndroidRuntime(26021):    at android.os.Handler.handleCallback(Handler.java:587)
05-25 20:06:58.025: E/AndroidRuntime(26021):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-25 20:06:58.025: E/AndroidRuntime(26021):    at android.os.Looper.loop(Looper.java:123)
05-25 20:06:58.025: E/AndroidRuntime(26021):    at android.app.ActivityThread.main(ActivityThread.java:4627)
05-25 20:06:58.025: E/AndroidRuntime(26021):    at java.lang.reflect.Method.invokeNative(Native Method)
05-25 20:06:58.025: E/AndroidRuntime(26021):    at java.lang.reflect.Method.invoke(Method.java:521)
05-25 20:06:58.025: E/AndroidRuntime(26021):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-25 20:06:58.025: E/AndroidRuntime(26021):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-25 20:06:58.025: E/AndroidRuntime(26021):    at dalvik.system.NativeStart.main(Native Method)

Main Activity:

public class Administrador  extends TabActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_administrador);

        TabHost tabHost = getTabHost();

        TabSpec photospec = tabHost.newTabSpec("Mantenimiento");
        photospec.setIndicator("Mantenimiento", getResources().getDrawable(R.drawable.icon_photos_tab));
        Intent photosIntent = new Intent(this, ListarAlumnos.class);
        photospec.setContent(photosIntent);

        TabSpec songspec = tabHost.newTabSpec("Asignaciones");
        songspec.setIndicator("Asignaciones", getResources().getDrawable(R.drawable.icon_songs_tab));
        Intent songsIntent = new Intent(this, Asignaciones.class);
        songspec.setContent(songsIntent);

        TabSpec videospec = tabHost.newTabSpec("Reportes");
        videospec.setIndicator("Reportes", getResources().getDrawable(R.drawable.icon_videos_tab));
        Intent videosIntent = new Intent(this, Reportes.class);
        videospec.setContent(videosIntent);

        // Adding all TabSpec to TabHost
        tabHost.addTab(photospec); // Adding photos tab
        tabHost.addTab(songspec); // Adding songs tab
        tabHost.addTab(videospec); // Adding videos tab



    }

}

Activity ListarAlumnos:

public class ListarAlumnos extends ActivityGroup implements OnItemClickListener{


    public static MenuAdministrador context; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.activity_listar_alumnos, null);
        this.setContentView(viewToLoad);

        //setContentView(R.layout.activity_listar_alumnos);




        lstalumnos = (ListView)findViewById(R.id.LISTAPRUEBA);
        lstalumnos.setOnItemClickListener(this);

        ///THE LISTVIEW CONTAINS DATA OF DATABASE 

    }



    public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {

        Toast.makeText(getApplicationContext(), "ok", 1000).show();
            //Context appContext = this.getApplicationContext();
            //Dialog builder = new Dialog(appContext);

        runOnUiThread(new Runnable() {

            public void run() {
                // TODO Auto-generated method stub

                final AlertDialog.Builder builder = new AlertDialog.Builder(getParent());
                ///builder.setTitle("Matenimiento de "+ Apellido.get(arg2));
                ///builder.setMessage("Por favor, escoger accion : ");
                ///builder.setPositiveButton("Actualizar", new DialogInterface.OnClickListener()
                ///{

                ///public void onClick(DialogInterface dialog, int which) {
                ///Actualizar(arg2);
                ///}
                ///});
                ///builder.setNegativeButton("Deshabilitar", new DialogInterface.OnClickListener() {

                ///public void onClick(DialogInterface dialog, int which) {
                ///     Eliminar(arg2);
                ///}
                ///});
                builder.create().show();            


            }})
        ;

    }

Solution

  • There are many question which handle the similar problem. There seems to be a problem with context. See:

    how to show alert inside an activity group?

    Dialog Box in Activity group inside a TabActivity not working

    Problems on an activitygroup under tabshost

    Hope this helps.