Dans mon application, j'ai un onglet et j'utilise ActivityGroup pour charger le contenu dans chaque onglet comme indiqué ci-dessous.Android: DatePicker ne fonctionne pas à l'intérieur Activité
public class FirstGroup extends ActivityGroup {
// Keep this in a static variable to make it accessible for all the nesten activities, lets them manipulate the view
public static FirstGroup group;
// Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.
private ArrayList<View> history;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.history = new ArrayList<View>();
group = this;
// Start the root activity withing the group and get its view
View view = getLocalActivityManager().startActivity("FlightsActivity", new
Intent(this,FlightsActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
// Replace the view of this ActivityGroup
replaceView(view);
}
public void replaceView(View v) {
// Adds the old one to history
history.add(v);
// Changes this Groups View to the new View.
setContentView(v);
}
i ont une image dans la classe d'activité FlightsActivity et sur l'événement onClick de l'image que je besoin d'un datepicker être displayed.I ont écrit le code pour cela et il fonctionnait très bien quand je spécifier directement FlightsActivity comme le contenu de l'onglet au lieu de le charger par le ActivityGroup.But maintenant je reçois une erreur
10-20 14:11:16.302: ERROR/AndroidRuntime(294): FATAL EXCEPTION: main
10-20 14:11:16.302: ERROR/AndroidRuntime(294): android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running?
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at android.view.ViewRoot.setView(ViewRoot.java:505)
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at android.view.Window$LocalWindowManager.addView(Window.java:424)
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at android.app.Dialog.show(Dialog.java:241)
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at android.app.DatePickerDialog.show(DatePickerDialog.java:129)
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at android.app.Activity.showDialog(Activity.java:2556)
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at android.app.Activity.showDialog(Activity.java:2514)
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at tabviewapp.com.FlightsActivity$10.onClick(FlightsActivity.java:166)
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at android.view.View.performClick(View.java:2408)
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at android.view.View$PerformClick.run(View.java:8816)
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at android.os.Handler.handleCallback(Handler.java:587)
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at android.os.Handler.dispatchMessage(Handler.java:92)
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at android.os.Looper.loop(Looper.java:123)
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at java.lang.reflect.Method.invokeNative(Native Method)
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at java.lang.reflect.Method.invoke(Method.java:521)
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-20 14:11:16.302: ERROR/AndroidRuntime(294): at dalvik.system.NativeStart.main(Native Method)
Voici mon code pour la mise en œuvre du datepicker:
mPickDate = (ImageView) findViewById(R.id.pickDate);
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
case DATE_DIALOG_ID_RETURN:
return new DatePickerDialog(this,
mDateSetListenerreturn,
mYear, mMonth, mDay);
}
return null;
}
// updates the date in the TextView
private void updateDisplay(TextView mDateDisplay) {
mDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mDay).append("-")
.append(mMonth + 1).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(mDateDisplay);
}
};
private DatePickerDialog.OnDateSetListener mDateSetListenerreturn =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay(mDateDisplayreturn);
}
};