關於這個問題有很多問題,但找不到任何具體問題,我有...onClick屬性在XML鏈接到Activity類中的方法
在我的layout.xml中,我使用標籤爲一個按鈕調用正確的onClickListener
。我得到的錯誤:
java.lang.IllegalStateException: Could not find a method handle_cancel(View) in the activity class com.matthieu.HelloWorldApplication for onClick handler on view class android.widget.Button with id 'button_cancel'
我在工作中使用的這種方法,但在擴展Application
類尋找它。我不明白爲什麼。視圖和所有僅在「活動」中設置的內容。
如果有人需要,這裏是方法的聲明(在我的活動,而不是在HelloWorldApplication):
public void handle_cancel(View v) {
// do something useful here
}
編輯(從adamp要求)......,可能回答我的問題:
這裏是第一個使用該佈局的部分代碼...
public class AddVocabularyActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.top); // that layout contains an empty LinearLayout id/main_content
}
private some_other_function() {
LinearLayout main_content = (LinearLayout) findViewById(R.id.main_content);
main_content.removeAllViews();
View.inflate(getApplicationContext(), R.layout.hello, main_content); // layout.hello is the one containing the button
}
// some other stuff
}
雖然複製/粘貼此代碼,我猜ŧ他的問題是,我以前getApplicationContext
膨脹與Button
查看...
您可以將代碼發佈到何處使用該佈局? – adamp