Android Studio中的OnClickListener

我正在尝试使用新的Android Studio开发和应用,但是我的OnClickListener始终收到重大错误。主要是告诉我它无法解析符号“

setOnClickListener”,也无法解析“ View v”

package com.sigmachi.derbydays;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

Button button= (Button) findViewById(R.id.standingsButton);

button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

startActivity(new Intent(MainActivity.this,StandingsActivity.class));

}

});

那是班上的代码

Information:Compilation completed with 11 errors and 0 warnings in 4 sec

Information:11 errors

Information:0 warnings

/Users/angelo/AndroidStudioProjects/SigmaChiDerbyDaysProject/SigmaChiDerbyDays/src/main/java/com/sigmachi/derbydays/MainActivity.java

Error:Error:line (28)Gradle: <identifier> expected

Error:Error:line (28)Gradle: illegal start of type

Error:Error:line (28)Gradle: ')' expected

Error:Error:line (28)Gradle: ';' expected

Error:Error:line (28)Gradle: invalid method declaration; return type required

Error:Error:line (30)Gradle: illegal start of type

Error:Error:line (30)Gradle: ';' expected

Error:Error:line (30)Gradle: ')' expected

Error:Error:line (30)Gradle: not a statement

Error:Error:line (30)Gradle: ';' expected

Error:Error:line (33)Gradle: illegal start of type

这些是我收到的错误,绝对没有道理。28行开始于button.setOnClickListener

编辑:现在当我按下按钮时,我收到一个强制关闭

这是应该打开的类,仅有的类是唯一的变化是要打开的布局

package com.sigmachi.derbydays;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

public class StandingsActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.standings_layout);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

回答:

Button button= (Button) findViewById(R.id.standingsButton);

button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

startActivity(new Intent(MainActivity.this,StandingsActivity.class));

}

});

此代码不是任何方法。如果要使用它,它必须位于类似OnCreate()

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button button= (Button) findViewById(R.id.standingsButton);

button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

startActivity(new Intent(MainActivity.this,StandingsActivity.class));

}

});

}

以上是 Android Studio中的OnClickListener 的全部内容, 来源链接: utcz.com/qa/402635.html

回到顶部