获取封闭对象的上下文

时间:2011-04-10 14:35:41

标签: java android eclipse

Java新手。我正在尝试创建一个显式的Intent来启动将用户发送到新选项卡并将一些数据传递给它。我认为我遇到的问题是获取我可以传递给Intent构造函数的父对象的Context。 “这个”不正确。 onCreate代码如下。任何指向示例的指针都会有所帮助

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quickactivity);

    final Button goButton = (Button) findViewById(R.id.go);
    goButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            Intent i = new Intent(this, TimeActivity.class);

        }
    });

}

2 个答案:

答案 0 :(得分:2)

您也可以使用:

Intent i = new Intent(v.getContext(), TimeActivity.class);

更多信息here

答案 1 :(得分:0)

使用

<MyClassName>.this

代替。替换此方法所在的实际类