2012-09-12 97 views
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 
+0

粘貼你的佈局xml – Bucks

+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 globaly和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); 
.... 
+0

謝謝SpK!我忽略了那個......愚蠢的我...... :)我會接受你的答案。 – omi0301

+0

@ omi0301嘗試一下,讓我知道。如果你有任何例外。 – Praveenkumar

+0

@ omi0301嘗試使用'et.append(...)'而不是'et.setText(...)' – Praveenkumar

0

你有NullPointerException異常,因爲你的EditText是ouside的「一」方法的範圍界定。

爲了避免這種異常,在類的範圍內定義(EditText et)並在onCreate方法中刪除定義。

0

onCreate()方法中刪除聲明EditText et
宣言初始化的EditTextonCreate方法已成爲當地由於這個。