IllegalStateException异常而铸造的上下文 [英] IllegalStateException while casting context

查看:127
本文介绍了IllegalStateException异常而铸造的上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想投一个活动到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 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.

解决方案

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.

这篇关于IllegalStateException异常而铸造的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆