-1

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


            }})
        ;

    }

1 Answers1

0

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.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • I think each activity should have a dialog box with their own context. This error errors when you are trying to open Dialog box when other the activity on which it should run is already ended. It might also occour when the Dialobox is showing and the activity from which it was opened gets destroyed. – Shobhit Puri May 26 '13 at 02:17
  • you're right! but the getParent () but not work for me and the truth and tried everything thanks for your answers! – Rosa María Torres Guerra May 26 '13 at 03:24
  • I also noticed that in the code above that you've only created the DialogBox. Where are you `dismissing` it? There should a place where you destroy/close it by using `.dismiss()`. It can be either on clicking any buttons in Dialog or any other way. But if you don't close it, it will surely give this error. – Shobhit Puri May 26 '13 at 03:36
  • Antes de usar tab corrió perfectamente, el problema surgio cuando le añadí el tab al principio y por consiguiente los ActivityGroup :( – Rosa María Torres Guerra May 26 '13 at 03:58
  • Before using tab ran perfectly, the problem arose when I added the tab at the beginning and therefore the ActivityGroup :( – Rosa María Torres Guerra May 26 '13 at 03:59
  • you still have the problem? – Shobhit Puri May 29 '13 at 06:10