Usman Kurd
Usman Kurd

Reputation: 7023

Unable to add Window error

i am using tabWidget. i have a date picker in my child class when i go first time from parent class to child class and select date it works fine and when i go again from parent class to child class and select date the app crashes with logcat result "Unable to add window"

it works fine for the first time but not for the next time code is following

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[]{
                    "[email protected]", "[email protected]",
                }
                ;
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                new String[] {
                    "[email protected]"
                }
                );
                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);
        }
    }
    ;
}

Upvotes: 3

Views: 1007

Answers (2)

kaneda
kaneda

Reputation: 6187

Instead of doing 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);

Upvotes: 1

user1285293
user1285293

Reputation: 108

In OnCreateDialog method, in the line:

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

use application basecontext instead of this.getparent()

it might help you.

Upvotes: 2

Related Questions