这是我的大问题:
http://www.bilder-space.de/show_img.php?img=7371f3-1333218887.jpg&size=original
和代码:
package org.madmax.sudoku;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class SudokuActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Set up Click Listener for all Buttons
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
}
}
}
}
错误是:
-void is an invalid type for the variable onCLick()
为什么eclipse会给我这个错误?
答案 0 :(得分:3)
您可以在onCreate方法中使用onclick,如下所示
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// your code here
}
});
}
答案 1 :(得分:2)
您需要从 onCreate 方法中带出 onClick 。编程中不允许嵌套方法声明。
package org.madmax.sudoku;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class SudokuActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Set up Click Listener for all Buttons
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
}
}
}
答案 2 :(得分:0)
您不能在Java中的另一个方法中使用方法。将onClick()
放在 onCreate()
之后,而不是 。
答案 3 :(得分:0)
您已在onClick
方法中定义了onCreate
方法。