我是一名新的android開發人員,我在應用程序中使用了ImageButton
,並在後臺添加了一些圖像。一切都看起來不錯,但是當我想在某個類中使用它並使用它下面的代碼時,它會強制關閉。ImageButton在類中不起作用
Help_Button = (Button) findViewById(R.id.Help_Button);
我是一名新的android開發人員,我在應用程序中使用了ImageButton
,並在後臺添加了一些圖像。一切都看起來不錯,但是當我想在某個類中使用它並使用它下面的代碼時,它會強制關閉。ImageButton在類中不起作用
Help_Button = (Button) findViewById(R.id.Help_Button);
Button
和ImageButton
彼此不同。您必須使用ImageButton
而不是Button
。
發佈您的佈局XML將有很大的幫助,但我敢打賭,你得到一個ClassCastException
努力蒙上了ImageButton
到Button
。試試這個instead.-
ImageButton helpButton = (ImageButton) findViewById(R.id.Help_Button);
hv你正確地設置了你的佈局?的setContentView()? – KOTIOS
請在你使用'Button'的地方發表更多的代碼。就像這個代碼所在的方法一樣,並確保它在'setContentView()'之後。另外添加logcat會很有幫助。 – codeMagic
也許顯示你是如何驗證'R.id.Help_Button'是否有你期望的信息? – jxh