我正在创建一种应用程序,其中有许多按钮,每个按钮都有不同的数字(0-9)。当我单击一个按钮时,我想将其编号显示到 edittext 字段。有点像一个计算器。下面是我的代码:
public class GameActivity extends Activity {
TextView timer;
Button button1;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameactivity);
Intent mIntent = getIntent();
int seconds = getIntent().getIntExtra("seconds", 0);
EditText et = (EditText) findViewById(R.id.editText1);
final TextView timer = (TextView) findViewById(R.id.textViewtimer);
new CountDownTimer(seconds, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("Seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
Intent intent = new Intent(GameActivity.this, Main.class);
startActivity(intent);
}
}.start();
}
public void one (View v){
int n = 1;
et.setText(n);
}
这是我得到的错误:
09-12 12:21:25.778: E/AndroidRuntime(3658): FATAL EXCEPTION: main
09-12 12:21:25.778: E/AndroidRuntime(3658): java.lang.IllegalStateException: Could not execute method of the activity
09-12 12:21:25.778: E/AndroidRuntime(3658): at android.view.View$1.onClick(View.java:2144)
09-12 12:21:25.778: E/AndroidRuntime(3658): at android.view.View.performClick(View.java:2485)
09-12 12:21:25.778: E/AndroidRuntime(3658): at android.view.View$PerformClick.run(View.java:9080)
09-12 12:21:25.778: E/AndroidRuntime(3658): at android.os.Handler.handleCallback(Handler.java:587)
09-12 12:21:25.778: E/AndroidRuntime(3658): at android.os.Handler.dispatchMessage(Handler.java:92)
09-12 12:21:25.778: E/AndroidRuntime(3658): at android.os.Looper.loop(Looper.java:123)
09-12 12:21:25.778: E/AndroidRuntime(3658): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-12 12:21:25.778: E/AndroidRuntime(3658): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 12:21:25.778: E/AndroidRuntime(3658): at java.lang.reflect.Method.invoke(Method.java:507)
09-12 12:21:25.778: E/AndroidRuntime(3658): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-12 12:21:25.778: E/AndroidRuntime(3658): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-12 12:21:25.778: E/AndroidRuntime(3658): at dalvik.system.NativeStart.main(Native Method)
09-12 12:21:25.778: E/AndroidRuntime(3658): Caused by: java.lang.reflect.InvocationTargetException
09-12 12:21:25.778: E/AndroidRuntime(3658): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 12:21:25.778: E/AndroidRuntime(3658): at java.lang.reflect.Method.invoke(Method.java:507)
09-12 12:21:25.778: E/AndroidRuntime(3658): at android.view.View$1.onClick(View.java:2139)
09-12 12:21:25.778: E/AndroidRuntime(3658): ... 11 more
09-12 12:21:25.778: E/AndroidRuntime(3658): Caused by: java.lang.NullPointerException
09-12 12:21:25.778: E/AndroidRuntime(3658): at com.dtan.mathwiz.GameActivity.one(GameActivity.java:42)
09-12 12:21:25.778: E/AndroidRuntime(3658): ... 14 more