IllegalStateException异常而铸造的上下文 [英] IllegalStateException while casting context
问题描述
我想投一个活动到FragmentActivty对象,以便我能得到FragmentManager对象
公共类主要扩展ListActivity {
...
公共无效showTimePickerDialog(视图v){
FragmentActivity myContext =(FragmentActivity)getApplicationContext(); //这里:java.lang.IllegalStateException:无法执行活动的方法 FragmentManager fragManager = myContext.getFragmentManager();
DialogFragment newFragment =新uSharedUtility.TimePickerFragment();
newFragment.show(fragManagertimePicker);
}
}
但是这样做,当我得到:
java.lang.IllegalStateException:无法执行活动的方法
我不能使用 getFragmentManager
直接,因为我的活动从 ListActivity
延伸,这是非常必要的。
请建议我来解决这个错误,我真的需要同时使用ListAdapter&放大器;日期/时间选择器在相同的活动。
我不能使用
getFragmentManager
直接,因为我的活动从ListActivity
延伸,这是非常必要的。
块引用>这是没有必要。你可以有一个
的ListView
的任何活动。既然你已经使用的片段,可以考虑使用
ListFragment
与FragmentActivity
。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 fromListActivity
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.
解决方案I cant use
getFragmentManager
directly because my activity extends fromListActivity
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 aFragmentActivity
.这篇关于IllegalStateException异常而铸造的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!