开发者

Getting the Context of the enclosing object

开发者 https://www.devze.com 2023-02-23 17:15 出处:网络
New to Java. I am trying to create an explicit Intent to launch send the user to a new tab and pass some data to it. The problem I think I am having is getting the Context of the parent object that I

New to Java. I am trying to create an explicit Intent to launch send the user to a new tab and pass some data to it. The problem I think I am having is getting the Context of the parent object that I can pass to the Intent Constructor. "This" is not correct. The onCreate code is below. Any pointers to examples would be helpful

public void onCreate(Bundle savedInstanceState) {
    super.onCreat开发者_运维知识库e(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);

        }
    });

}


You can also use:

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

More info here


Use

<MyClassName>.this

instead. Replacing by the actual class in which this method resides

0

精彩评论

暂无评论...
验证码 换一张
取 消