Android 我如何为按钮创建新的意图以打开新类?
这是我的代码,我需要在{}中放置一些东西,将按钮链接到新类或活动,如second page.java:Android 我如何为按钮创建新的意图以打开新类?,android,eclipse,android-intent,Android,Eclipse,Android Intent,这是我的代码,我需要在{}中放置一些东西,将按钮链接到新类或活动,如second page.java: public void addListenerOnButton() { ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton1); imageButton.setOnClickListener(new OnClickListener() { /*What can I p
public void addListenerOnButton() {
ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
/*What can I put here to open new class
,I mean another activity like secondp.java.*/
}
我试图输入以下代码,但出现以下错误:
The constructor Intent(new View.OnClickListener(){}, Class<List>) is undefined
尝试删除“this”子句,因为在代码中使用“this”时,它引用的是onclicklistener类。如果要在上下文中添加,请在方法之前引用上下文,然后使用它。更改
Intent k = new Intent(this,Secondp.class);
到
只需使用这个
关键字,就可以传递OnClickListener的对象
Simple
您使用了错误的此:)
当需要上下文时,此
将在OnClickListener
类中传递给Intent
使用:
startActivity(new Intent(NameofyourcurrentActivity.this,Second.class));
在本例中,此
传递的是侦听器,而不是视图。另外,arg0
,a没有getApplicationContext()
方法,但是它有getContext()
@a--C它与getApplicationContext是同一个上下文吗?应该是按钮定义的上下文,所以活动上下文。然而,以这种方式开始一项活动与它无关——所有上下文都可以开始活动,意图只需要传递一个上下文即可。@DheeB非常感谢,它刚刚起作用。之前的答案不起作用。@–A--C我对它们做了一些研究,最后两个都成功了。Thanx
Intent k = new Intent(NameofyourcurrentActivity.this,Secondp.class);
startActivity(new Intent(NameofyourcurrentActivity.this,Second.class));