0

我正在创建一种应用程序,其中有许多按钮,每个按钮都有不同的数字(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
4

3 回答 3

0

因为这 -

....
int seconds = getIntent().getIntExtra("seconds", 0);

EditText et = (EditText) findViewById(R.id.editText1);           // Already declared in global
final TextView timer = (TextView) findViewById(R.id.textViewtimer);
....

您声明了全局并使用新声明进行了EditText初始化。onCreate该方法one方法仅考虑EditText来自全局对象的 ' 对象。所以,它(et)是空的。因此,最好删除其中的声明onCreate并执行如下代码 -

....
int seconds = getIntent().getIntExtra("seconds", 0);

et = (EditText) findViewById(R.id.editText1);
final TextView timer = (TextView) findViewById(R.id.textViewtimer);
....
于 2012-09-12T04:22:39.037 回答
0

您得到 NullPointerException 因为您的 EditText 是在“one”方法的范围之外定义的。

为避免此异常在类范围内定义(EditText 等)并在 onCreate 方法中删除定义。

于 2012-09-12T04:26:17.007 回答
0

删除方法中的声明。由于这个原因,方法中的声明初始化已经成为本地的。EditText etonCreate()
EditTextonCreate

于 2012-09-12T04:26:44.010 回答