2012-04-24 85 views
3

我正在使用tabWidget。我有一個日期選擇器在我的子類中,當我第一次從父類到子類和選擇日期,它工作正常,當我再次從父類到子類和選擇日期應用程序崩潰logcat結果「無法添加窗口」無法添加窗口錯誤

正常工作的第一次,但接下來的日子不適合 代碼以下

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); 
     } 
    } 
    ; 
} 
+0

其尋找ungly。首先,自己設置代碼的格式,以便其他人易於閱讀 – 2012-04-24 07:05:14

回答

3

OnCreateDialog方法,在該行:

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

使用application basecontext而不是this.getparent()

它可能會幫助你。

+0

您可以請一個虛擬示例更詳細一些嗎? – 2012-04-24 07:32:07

+0

我應該使用getbaseContext()然後getParent()或者我應該使用getApplicationContext然後getParent()? – 2012-04-24 10:33:34

1

而不是做這個的:

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

試試這個,看看它是否工作:

return new DatePickerDialog(PlaceOrder.this, datePickerListener, year, month, 
       day); 
相關問題