androiddialogactivitygroupandroid-tabactivity

Dialog Box in Activity group inside a TabActivity not working


I'm trying to run an ActivityGroup embedded inside another ActivityGroup. (Example : an ActivityGroup inside a TabActivity)

This seems to display properly but throw an exception when I'm trying to display a modal box like an AlertDialog.

TabActivity

If childActivity1 or 2 try to display a DialogBox I'm getting this exception :

ERROR/AndroidRuntime(2449): 
android.view.WindowManager$BadTokenException: Unable to add window -- 
token android.app.LocalActivityManager$LocalActivityRecord@43424620 is 
not valid; is your activity running? 

If Anybody know please reply...

This is my dialog box code

           final Dialog dialog = new Dialog(EditDeleteReqCardList.this);
  dialog.setContentView(R.layout.customdialougbox);
        dialog.setTitle("Request Card - "+reqid);

        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Do You Want to Delete This request Card ?");
        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.dialougimage);

    Button dialogButton = (Button)    dialog.findViewById(R.id.dialogButtonOK);
        Button cancelbutton = (Button) dialog.findViewById(R.id.cancel);
        dialogButton.setText("Yes");
                   dialog.show();

My GroupActivity

       public class RequestcardList  extends ActivityGroup  {

 private ArrayList<String> mIdList;


protected static final int SUB_ACTIVITY_REQUEST_CODE = 100;

int finalresultlengt1;
  String[][] outputString=null;
  String upid="";
  String group_id="";
  String result="";
  String travel_date="";
  String travel_from="";
  String travel_to="";
  String travel_start_time="";
  String ampm="";
 String card_type="";
  String vacancycard_id="";
  private ArrayList<HashMap<String,String>> list;
  String FIRST_COLUMN = Constant.FIRST_COLUMN;
  String SECOND_COLUMN = Constant.SECOND_COLUMN;
  String THIRD_COLUMN = Constant.THIRD_COLUMN;
  String FOURTH_COLUMN = Constant.FOURTH_COLUMN;
  String vacanyid_seat="";
  int groupid =0;
public void onCreate(Bundle savedInstanceState) 

{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.request_mainlist);

    if (mIdList == null) mIdList = new ArrayList<String>();

         final ListView lview = (ListView) findViewById(R.id.listView);

         TextView heading=(TextView)findViewById(R.id.heading);
         TextView column1=(TextView)findViewById(R.id.column1);
         TextView column2=(TextView)findViewById(R.id.column2);
        /*populateList();
        listviewAdapter adapter = new listviewAdapter(this, list);
        lview.setAdapter(adapter);*/

     SharedPreferences myprefs1= getSharedPreferences("user", MODE_WORLD_READABLE);
     upid= myprefs1.getString("id",null);
     String group_id=myprefs1.getString("group_id", null);
     int groupid = Integer.parseInt(group_id);
     System.out.println("group id"+group_id);
     System.out.println("up id"+upid);

     if(groupid==1)
     {
         heading.setText("REQUEST CARD LIST");
         column1.setText("ReqID");
         column2.setText("Requested Date");
         System.out.println("1");
         card_type="Requestcard";
         vacanyid_seat="vacancycard_id";
          System.out.println("card_type"+card_type);

     }
     else
     {
         heading.setText("VACANCY CARD LIST");
         column1.setText("VacID");
         column2.setText("Vacant Date");
         System.out.println("2");
         card_type="vacancycards" ;
         vacanyid_seat="seats_available";
         System.out.println("card_type"+card_type);
     }
     HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
        HttpResponse response;
        //String url= "http://10.0.2.2/Mebuddie/logins/login11";

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("upid", upid));
        nameValuePairs.add(new BasicNameValuePair("cardtype", card_type));

             try{
                 HttpClient httpclient = new DefaultHttpClient();
                   HttpPost httppost = new HttpPost("http://10.0.2.2/Mebuddie/userprofiles/cardsJson");

                   httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
                    response = httpclient.execute(httppost);
                    StringBuilder builder = new StringBuilder();
                 BufferedReader   reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));

                    for (String line = null; (line = reader.readLine()) != null;) {
                        builder.append(line).append("\n");
                        System.out.println("builderrrrrrrr"+builder.toString());
                    }


                 JSONTokener tokener = new JSONTokener(builder.toString());


                  JSONArray finalResult = new JSONArray(tokener);
                   finalresultlengt1=finalResult.length();
                   System.out.println("tokener"+tokener.toString());
              System.out.println("final result length"+finalresultlengt1);
                    Object type = new Object();
                    if (finalResult.length() == 0 && type.equals("both")) 
                    {
                        System.out.println("null value in the json array");


                    }
                    else {
                        int i,j;

                        outputString= new String[finalresultlengt1][8];
                         JSONObject json_data = new JSONObject();

                            for (i = 0; i < finalresultlengt1; i++) 
                            {

                                 json_data = finalResult.getJSONObject(i);
                                 JSONObject menuObject = json_data.getJSONObject(card_type);
                                  upid=   menuObject.getString("id");   
                                  travel_date=   menuObject.getString("travel_date");   
                                  travel_from=   menuObject.getString("travel_from");   
                                  travel_to=   menuObject.getString("travel_to");   
                                  travel_start_time=   menuObject.getString("travel_start_time");   
                                  ampm=   menuObject.getString("ampm"); 

                                  vacancycard_id=   menuObject.getString(vacanyid_seat);    


                                // System.out.println("value of i"+i);





                             //   System.out.println("json dataa"+json_data.names().toString());



                            /* System.out.println(upid);
                              System.out.println(travel_date);
                              System.out.println(travel_from);
                              System.out.println(travel_to);
                              System.out.println(travel_start_time);
                              System.out.println(ampm);
                              System.out.println(vacancycard_id);

                              outputString[i][j]=upid;
                                         System.out.println("value of j after upid"+j);
                                        outputString[i][j]=travel_date;
                                        outputString[i][j]=travel_from;
                                        outputString[i][j]=travel_to;
                                        outputString[i][j]=travel_start_time;
                                        outputString[i][j]=ampm;
                                        outputString[i][j]=vacancycard_id;

                                       System.out.println(outputString[i][j]); 

                              */

                                        outputString[i][0]=upid;
                                        // System.out.println("value of j after upid"+j);
                                        outputString[i][1]=travel_date;
                                        outputString[i][2]=travel_from;
                                        outputString[i][3]=travel_to;
                                        outputString[i][4]=travel_start_time;
                                        outputString[i][5]=ampm;
                                        outputString[i][6]=vacancycard_id;

                                      // System.out.println(i+outputString[i][5]); 

                             //  System.out.println(outputString[i][j]);   



                            }

                               System.out.println("length of mdialoug"+outputString.length);
                               System.out.println("value at 5th position "+outputString[0][5]); 

                          //    Toast.makeText(RequestcardList.this,result,Toast.LENGTH_LONG).show();
                              int a,b;
                            list = new ArrayList<HashMap<String,String>>();
                            for ( a=0; a<outputString.length;a++)
                            {



                                    HashMap<String,String> temp1 = new HashMap<String,String>();
                                        temp1.put(FIRST_COLUMN,outputString[a][0]);
                                        temp1.put(SECOND_COLUMN, outputString[a][1]);
                                        temp1.put(THIRD_COLUMN, outputString[a][4]);
                                        temp1.put(FOURTH_COLUMN, outputString[a][5]);
                                    list.add(temp1);
                                     //System.out.println("outputString"+ outputString[a][b]);
                            }

                            listviewAdapter adapter = new listviewAdapter(this, list);
                            lview.setAdapter(adapter);
                          lview.setOnItemClickListener(new OnItemClickListener() {

                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                                 HashMap<String, String> map = (HashMap<String, String>) parent.getItemAtPosition(position);

                                  String selectedId = map.get(FIRST_COLUMN);
                                 String two = map.get(FOURTH_COLUMN);

                                long s=lview.getItemIdAtPosition(position);
                                System.out.println(s);
                                int pos=(int)s;

                            String reqid=outputString[pos][0];
                            String tdate=outputString[pos][1];
                            String tfrom=outputString[pos][2];
                            String tTo=  outputString[pos][3];
                            String stime=outputString[pos][4];
                            String time= outputString[pos][5];
                            String vid=  outputString[pos][6];

                           Intent i=new Intent( RequestcardList.this,EditDeleteReqCardList.class);



                           Bundle reqdetails = new Bundle();
                            reqdetails.putString("reqid", reqid);
                            reqdetails.putString("tdate", tdate);
                            reqdetails.putString("tfrom", tfrom);
                            reqdetails.putString("tTo", tTo);
                            reqdetails.putString("stime",stime);
                            reqdetails.putString("time", time);
                            reqdetails.putString("vid", vid);
                           reqdetails.putString("cardtype", card_type);

                         i.putExtras(reqdetails);
                        startChildActivity("EditDeleteReqCardList", i);


                            Toast.makeText(RequestcardList.this,two,Toast.LENGTH_SHORT).show();

                            //startActivity(i);

                            }
                        });

                    }

             }

         catch (Exception e) {

      Toast.makeText(RequestcardList.this,"no card presents",Toast.LENGTH_LONG).show();
                 e.printStackTrace();
                    }


}


@Override
  public void finishFromChild(Activity child) {
      LocalActivityManager manager = getLocalActivityManager();
      int index = mIdList.size()-1;

      if (index < 1) {
              finish();
              return;
          }

          manager.destroyActivity(mIdList.get(index), true);
          mIdList.remove(index);
          index--;
          String lastId = mIdList.get(index);
          Intent lastIntent = manager.getActivity(lastId).getIntent();
          Window newWindow = manager.startActivity(lastId, lastIntent);
          setContentView(newWindow.getDecorView());
  }


public void startChildActivity(String Id, Intent intent) {     
      Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
      if (window != null) {
          mIdList.add(Id);
          setContentView(window.getDecorView()); 
      }    
  }


@Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_BACK) {
          //preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
          return true;
      }
      return super.onKeyDown(keyCode, event);
  }
@Override
  public boolean onKeyUp(int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_BACK) {
          onBackPressed();
          return true;
      }
      return super.onKeyUp(keyCode, event);
  }

 @Override
  public void  onBackPressed  () {
      int length = mIdList.size();
      if ( length >=1) {
          Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
          current.finish();
      }
  }


}

Solution

  • use final Dialog dialog = new Dialog(getParent());