user971741
user971741

Reputation:

IllegalStateException while casting context

I am trying to cast an activity to a FragmentActivty object so I could get FragmentManager object

public class Main extends ListActivity {
...
   public void showTimePickerDialog(View v) {
        FragmentActivity myContext=(FragmentActivity) getApplicationContext(); //Here: java.lang.IllegalStateException: Could not execute method of the activity

        FragmentManager fragManager = myContext.getFragmentManager();
        DialogFragment newFragment = new uSharedUtility.TimePickerFragment();
        newFragment.show(fragManager, "timePicker");
    }
}

But when doing it I get:

java.lang.IllegalStateException: Could not execute method of the activity

I cant use getFragmentManager directly because my activity extends from ListActivity which is very necessary.

Please suggest me to get around this error, i really need to use both ListAdapter & Date/Time pickers in the same activity.

Upvotes: 0

Views: 111

Answers (2)

laalto
laalto

Reputation: 152787

I cant use getFragmentManager directly because my activity extends from ListActivity which is very necessary.

It isn't necessary. You can have a ListView in any activity.

Since you're already using fragments, consider using a ListFragment with a FragmentActivity.

Upvotes: 0

Ganesh AB
Ganesh AB

Reputation: 4698

You can't cast Application Context to Activity, you must need Activity Context.

(FragmentActivity) getApplicationContext() // Not Possible

getContext(): Returns the context the view is currently running in. Usually the currently active Activity.

getApplicationContext(): Returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity.

Upvotes: 0

Related Questions