android context的别

来源:互联网 发布:淘宝全球购举报 编辑:程序博客网 时间:2024/05/02 10:48
if (isChecked) {                    if (temp != -1) {                        RadioButton tempButton = (RadioButton) ((Activity)context_).findViewById(temp);                        if (tempButton != null) {                            tempButton.setChecked(false);                        }                    }                    temp = buttonView.getId();               }

这是在一个继承baseAdapter的自定义适配器类(不是acitvity中的内部类)里的getView方法中,

其它上面的这代码的context_必须为那个activity类的this,或是activity类名.this,通过构造方法传入这个适配器类中,不能是baseContext(),或是getApplicationContext(),如果是后两者就会报classCaseException异常,也就是说会类型转换错误!


public View getView( int arg0, View arg1, ViewGroup arg2) {if(Flag){if(arg1==null){LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

而这代码中的context,必须是那个activity中getApplicattonContext(),不能是getBaseContext(),或是Activty的context,如果是两者,程序就会报NullException异常,

注:我的那个activity界面是通过一个inflater引导进来了,


原创粉丝点击