gpt4 book ai didi

android - Activity 组的子 Activity 中的日期选择器引发 BadTokenException 错误

转载 作者:行者123 更新时间:2023-11-29 00:29:36 25 4
gpt4 key购买 nike

我需要在选项卡式 Activity 中的 Activity 组的子 Activity 中显示两个日期选择器。两个 TextView 和显示日期的按钮的代码是:

  incorp_date=(TextView)findViewById(R.id.edt_incorpdate);
incorp_date_image=(Button)findViewById(R.id.incorp_date);


incorp_date_cal=Calendar.getInstance();

incorp_date_image.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
showDialog(DATE_PICKER_INCORP);
}
});

final Calendar c = Calendar.getInstance();
incorp_year = c.get(Calendar.YEAR);
incorp_month = c.get(Calendar.MONTH);
incorp_day = c.get(Calendar.DAY_OF_MONTH);

/* display the current date (this method is below) */
updateIncorpDisplay();


estb_date=(TextView)findViewById(R.id.edt_estabdate);
estb_date_image=(Button)findViewById(R.id.estb_date);
estb_date_cal=Calendar.getInstance();

estb_date_image.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
showDialog(DATE_PICKER_ESTB);
}
});
final Calendar c1 = Calendar.getInstance();
estb_year = c1.get(Calendar.YEAR);
estb_month = c1.get(Calendar.MONTH);
estb_day = c1.get(Calendar.DAY_OF_MONTH);

/* display the current date (this method is below) */
updateEstbDisplay();

显示日期选择器对话框的代码是:

incorp_dateListener=new OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
incorp_year = year;
incorp_month = monthOfYear;
incorp_day = dayOfMonth;
updateIncorpDisplay();
}
};

estb_dateListener=new OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
estb_year = year;
estb_month = monthOfYear;
estb_day = dayOfMonth;
updateEstbDisplay();
}
};
@Override
protected Dialog onCreateDialog(int id) {

switch(id){
case DATE_PICKER_INCORP:
return new DatePickerDialog(getParent(), incorp_dateListener, incorp_year, incorp_month, incorp_day);
case DATE_PICKER_ESTB:
return new DatePickerDialog(getParent(), estb_dateListener, estb_year, estb_month, estb_day);
}
return null;
}

我无法显示日期选择器对话框。当我单击按钮并引发 BadTokenException 异常时,应用程序强制关闭。我应该怎么办???我似乎无法弄清楚问题是什么???我想也许是因为它是一个 Activity 组的子 Activity ..但找不到任何相关的解决方案..请帮助!!!!

最佳答案

尝试这样的事情,首先你在你的 TabActivity 类中创建 tabcontext 对象。像这样

package com.loanreminder;

import android.app.TabActivity;
import android.os.Bundle;

/**
* @author Adil Soomro
*
*/
public class TabSample extends TabActivity {
/** Called when the activity is first created. */
public static TabSample tabContext;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost);
tabContext = this;

}

}

在你的 Activity 中像这样使用这个对象之后。

protected Dialog onCreateDialog(int id) {

switch(id){
case DATE_PICKER_INCORP:
return new DatePickerDialog(TabSample.tabContext, incorp_dateListener, incorp_year, incorp_month, incorp_day);
case DATE_PICKER_ESTB:
return new DatePickerDialog(TabSample.tabContext, estb_dateListener, estb_year, estb_month, estb_day);
}
return null;
}

关于android - Activity 组的子 Activity 中的日期选择器引发 BadTokenException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16685115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com