Failed to add window error

I am using tabWidget. I have a date picker in my child class the first time I switch from the parent class to the child class and select the date when it works fine, and when I again switch from the parent class to the child class and select the date when the application crashes with logcat result "Failed to add window"

it works fine the first time, but not the next time the following code

import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class PlaceOrder extends Activity {
    public TextView tvDisplayDate,personName,Cname,cAdrress;
    public DatePicker dpResult;
    public Button btnChangeDate;
    Button cancel,submit;
    public int year;
    public int month;
    public int day;
    int requestCode;
    int resultCode;
    Intent data;
    String Name,cName,Title,Address,Phone,Email;
    static final int DATE_DIALOG_ID = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.placeorder);
        personName=(TextView)findViewById(R.id.firstName);
        Cname=(TextView)findViewById(R.id.Cname);
        cAdrress=(TextView)findViewById(R.id.CAddname);
        personName.setText(AccountInfo.fnameNlName);
        Cname.setText(AccountInfo.cName);
        cAdrress.setText(AccountInfo.cAdd);
        setCurrentDateOnView();
        addListenerOnButton();
        final PlaceOrder PO = this;
        cancel=(Button)findViewById(R.id.Cancel);
        cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(FastPkgMainActivity.fastPackge==1)
                {
                    Intent fastPkgQuote = new Intent(v.getContext(), FastPkgQuote.class);
                    StringBuffer urlString = new StringBuffer();
                    FastPkgQuote parentActivity = (FastPkgQuote)getParent();
                    parentActivity.replaceContentView("fastpkg1", fastPkgQuote);
                }
                else if(FastPkgMainActivity.fastPackge==2)
                {
                    Intent fastpkgQuoteNew = new Intent(v.getContext(), FastPkgQuoteNew.class);
                    StringBuffer urlString = new StringBuffer();
                    FastPkgQuoteNew parentActivity = (FastPkgQuoteNew)getParent();
                    parentActivity.replaceContentView1("fastPkg2", fastpkgQuoteNew);
                }
                // TO*DO Auto-generated method stub
            }
        }
        );
        submit=(Button)findViewById(R.id.Submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Name=AccountInfo.fnameNlName.toString();
                    cName=AccountInfo.cName.toString();
                    Title=AccountInfo.title.toString();
                    Address=AccountInfo.cAdd.toString();
                    Phone=AccountInfo.phno.toString();
                    Email=AccountInfo.E_ID.toString();
                }
                catch (NullPointerException e) {
                    Name=" ";
                    cName=" ";
                    Title=" ";
                    Address=" ";
                    Phone=" ";
                    Email=" ";
                }
                // TODO Auto-generated method stub
                Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                //String[] recipients = new String[]{
                    "mymail@email.com", "sales@fastpkg.com",
                }
                ;
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                new String[] {
                    "sales@fastpkg.com"
                }
                );
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Placing an Order");
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "CustomerDetails       " +
                " Name"+":  "+Name+
                "CompanyName:"+"  "+cName+
                "Title :"+Title+
                "Address: "+Address+
                "Phone #: "+Phone+
                "Email :"+Email
                );
                emailIntent.setType("text/plain");
                startActivityForResult(Intent.createChooser(emailIntent, "Send mail client :"),RESULT_OK);
                onActivityResult(requestCode, resultCode, data);
            }
        }
        );
    }
    protected void onActivityResult(int requestCode, int resultCode,
    Intent data) {
        if (requestCode == 0) {
            if (resultCode == RESULT_CANCELED) {
                if(FastPkgMainActivity.fastPackge==1)
                {
                    Intent fastpkgQuote = new Intent();
                    StringBuffer urlString = new StringBuffer();
                    fastpkgQuote.setClass(getParent(), FastPkgQuote.class);
                    FastPkgQuote parentActivity = (FastPkgQuote)getParent();
                    parentActivity.replaceContentView("fastPkg2", fastpkgQuote);
                }
                else if(FastPkgMainActivity.fastPackge==2)
                {
                    Intent fastpkgQuoteNew = new Intent();
                    StringBuffer urlString = new StringBuffer();
                    fastpkgQuoteNew.setClass(getParent(),
                    FastPkgQuoteNew.class);
                    FastPkgQuoteNew parentActivity =
                    (FastPkgQuoteNew)getParent();
                    parentActivity.replaceContentView1("fastPkg2",
                    fastpkgQuoteNew);
                }
            }
            else if(resultCode==RESULT_OK)
            {
                final PlaceOrder pOrder = this;
                Intent intent = new Intent();
                intent.setClass(pOrder, ThnkYouPage.class);
                startActivity(intent);
            }
        }
    }
    // display current date
    public void setCurrentDateOnView() {
        tvDisplayDate = (TextView) findViewById(R.id.tvDate);
        dpResult = (DatePicker) findViewById(R.id.dpResult);
        dpResult.setVisibility(View.INVISIBLE);
        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        // set current date into textview
        tvDisplayDate.setText(new StringBuilder()
        // Month is 0 based, just add 1
        .append(month + 1).append("-").append(day).append("-")
        .append(year).append(" "));
        // set current date into datepicker
        dpResult.init(year, month, day, null);
    }
    public void addListenerOnButton() {
        btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
        btnChangeDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        }
        );
    }
    @Override
    public  Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:
            // set date picker as current date
            return new DatePickerDialog(this.getParent(), datePickerListener, year, month,
            day);
        }
        return null;
    }
    public  DatePickerDialog.OnDateSetListener datePickerListener = new
    DatePickerDialog.OnDateSetListener() {
        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
        int selectedMonth, int selectedDay) {
            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;
            // set selected date into textview
            tvDisplayDate.setText(new StringBuilder().append(month + 1)
            .append("-").append(day).append("-").append(year)
            .append(" "));
            // set selected date into datepicker also
            dpResult.init(year, month, day, null);
        }
    }
    ;
}
+3
source share
2 answers

In the OnCreateDialogmethod in the line:

return new DatePickerDialog(this.getParent(), datePickerListener, year, month,day);

use application basecontextinsteadthis.getparent()

it can help you.

+3
source

Instead of this:

return new DatePickerDialog(this.getParent(), datePickerListener, year, month,
            day);

Try this and see if it works:

return new DatePickerDialog(PlaceOrder.this, datePickerListener, year, month,
                day);
+1
source

All Articles