In my application I have a Tabbar and I am using ActivityGroup to load contents into each tab as shown below.
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 have an image inside the FlightsActivity activity class and on the onClick event of the image I need a datepicker to be displayed.I have written the code for that and it was working fine when I was directly specifying FlightsActivity as the content of the tab instead of loading it through the ActivityGroup.But now i am getting an error
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 android.app.LocalActivityManager$LocalActivityRecord@43e497e0 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)
Below is my code for implementing the 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 n开发者_StackOverflow中文版ew 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);
}
};
Try:
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(getParent(),
mDateSetListener,
mYear, mMonth, mDay);
case DATE_DIALOG_ID_RETURN:
return new DatePickerDialog(getParent(),
mDateSetListenerreturn,
mYear, mMonth, mDay);
}
this
isn't the main context
in this case.
Try this to display your context
:
Instead of :
setContentView(R.layout.xyz);
Use this:
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.xyz, null);
this.setContentView(viewToLoad );
This should work:
private void setInputDatePickerDialog(Activity activity) {
dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
Calendar newCalendar = Calendar.getInstance();
inputDatePickerDialog = new DatePickerDialog(activity, //<-- this is the best
精彩评论