我正在创建一种应用程序,其中有许多按钮,每个按钮具有不同的数字(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
答案 0 :(得分: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);
....
答案 1 :(得分:0)
你得到了NullPointerException,因为你的EditText被定义为“one”方法的范围。
要避免此异常,请在类的范围内定义(EditText et)并删除onCreate方法中的定义。
答案 2 :(得分:0)
删除EditText et
方法中onCreate()
的声明
由于此原因,EditText
方法中的{{1>}的{em>声明和初始化已成为本地。