大家好,最近開始練習寫Android,但遇到一些問題,
還在做一些基本的功能,
正題: 我寫了一個輸入姓名與電話號碼並顯示出來的小程式,
code 大概如下(系統產生的就忽略不放)
public class MainActivity extends ActionBarActivity {
... ... ... ... ... ...
EditText sname, fname, phone;
TextView txv;
protected void onCreate(Bundle savedInstanceState) {
... ... ... ...
sname = (EditText)findViewById(R.id.surname);
fname = (EditText)findViewById(R.id.firstname);
phone = (EditText)findViewById(R.id.phone);
txv = (TextView)findViewById(R.id.txv);
}
... ... ... ... ...
public void onclick(View v){
txv.setText(sname.getText().toString() + fname.getText().toString() + "的
電話是" + phone.getText());
}
}
有點亂,但大概意思就是把變數在onCreate中初始化,然後在副程式中執行,
但是這樣執行會錯誤,就當掉結束了,
不過若把變數初始化都放到副程式中,這樣就可以運作,請問這是怎樣的情形阿?
系統出現的錯誤訊息大概是:
java.lang.IllegalStateException: Could not execute method of the activity
(我不懂為啥不能執行耶)
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.NullPointerException
大概這幾行說明
在請各位大大指導一下!
我還在摸索Log.e這個東西,還不是很會用,因此想說先上來請教一下,感謝各位。