【发布时间】:2011-07-06 02:38:53
【问题描述】:
关于这个主题有很多问题,但找不到与我遇到的具体问题有关的任何问题......
在我的layout.xml 中,我使用android:onClick 标记作为按钮来调用正确的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'
我在 Activity 中实现了该方法,但它正在扩展 Application 的类中寻找它...我不明白为什么。视图和所有仅在 Activity 中设置的内容。
如果有人需要,这里是该方法的声明(在我的活动中,而不是在 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 填充视图...
【问题讨论】:
-
你能把代码贴在你使用那个布局的地方吗?
标签: android android-layout onclick