Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我如何为按钮创建新的意图以打开新类?_Android_Eclipse_Android Intent - Fatal编程技术网

Android 我如何为按钮创建新的意图以打开新类?

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

这是我的代码,我需要在{}中放置一些东西,将按钮链接到新类或活动,如second page.java:

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));