我正在使用此链接在我的 android 应用程序中提供日期选择器
但我收到此错误:
05-21 10:19:08.712: E/AndroidRuntime(13265): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@40d42048 is not valid; is your activity running?
05-21 10:19:08.712: E/AndroidRuntime(13265): at android.view.ViewRootImpl.setView(ViewRootImpl.java:567)
05-21 10:19:08.712: E/AndroidRuntime(13265): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
05-21 10:19:08.712: E/AndroidRuntime(13265): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
05-21 10:19:08.712: E/AndroidRuntime(13265): at android.app.Dialog.show(Dialog.java:281)
05-21 10:19:08.712: E/AndroidRuntime(13265): at android.app.Activity.showDialog(Activity.java:3071)
05-21 10:19:08.712: E/AndroidRuntime(13265): at android.app.Activity.showDialog(Activity.java:3021)
我该如何解决这个错误???请帮忙!!!
这是我的java代码:
public class Applicant extends ActivityGroup {
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 1;
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
}
}
private void updateDisplay() {
tv_dob.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
ArrayList<String> city=new ArrayList<String>();
EditText applicatn_formno,fname,mname,lname,addrs1,addrs2,landmrk,
pincode,stdcode,telephone,mobile;
Button btn_next,pickDate;
Spinner spin_occupation;
TextView tv_dob,tv_applndate;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.applicant);
spin_occupation=(Spinner)findViewById(R.id.spinner_ocupa);
tv_dob=(TextView)findViewById(R.id.edt_dateofbirth);
tv_applndate=(TextView)findViewById(R.id.applnDate);
pickDate=(Button)findViewById(R.id.pickDate);
current_date=DateFormat.getDateTimeInstance().format(new Date());
tv_applndate.setText(current_date);
pickDate.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("deprecation")
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplay();
btn_next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), ""+ str_state, Toast.LENGTH_LONG).show();
if(str_permant.equalsIgnoreCase("No"))
{
Intent activity=new Intent(v.getContext(),PermanentAddress.class);
replaceContentView("activity", activity);
}
else{
Intent activity2=new Intent(v.getContext(),Applicant2.class);
replaceContentView("activity2", activity2);
}
}
});
}
} xml 文件(这只是 datepicker 的相关代码)
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
<Button
android:id="@+id/pickDate"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/edt_dateofbirth"
android:background="@drawable/calendaricon" />
<TextView
android:id="@+id/edt_dateofbirth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Date of Birth"
android:textSize="14sp" />
</RelativeLayout>