我正在使用 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[]{
"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);
}
}
;
}