[問題] 新手請教問題

作者: chienchan (linting)   2014-06-20 22:26:24
大家好,最近開始練習寫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這個東西,還不是很會用,因此想說先上來請教一下,感謝各位。
作者: passli   2014-06-20 22:49:00
沒有setContentView(R.layout.xxx); ?
作者: chienchan (linting)   2014-06-20 23:03:00
有耶!~
作者: passli   2014-06-20 23:51:00
那你的Click 是怎麼設的?
作者: keieykdx (YOz桑)   2014-06-21 07:39:00
省略程式碼不能解決問題, 請參考至底來分享問題
作者: BearrrXD (BearrrXD)   2014-06-21 12:15:00
你layout裡面元件屬性設置的onclick跟你activity裡面設的onclick(View v) Method名稱要一樣

Links booklink

Contact Us: admin [ a t ] ucptt.com