我怎么一个按钮分配给Android的活动? [英] How do I assign a Button to an activity in Android?
问题描述
我非常新到Android和Java,但这个网站已经帮我到现在为止,因此非常感谢大家!
我构建有两个选项卡,在一个选项卡我已经创造了一些按钮,并在相应的活动我有一个OnClickListener的应用程序。当我运行的应用程序它迫使接近,我得到的错误:
ERROR / AndroidRuntime(25971):java.lang.IllegalStateException:找不到在活动课com.test.rate.MainActivity对视图的onClick处理程序的方法myClickHandler(视图)类android.widget.Button ID为CalculateButton
这也可能是真的简单,但帮助!
好吧,是我应该补充一些code:
活动:
公共类MetricActivity延伸活动{
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 的setContentView(R.layout.metriclayout);
最后Button按钮=(按钮)findViewById(R.id.CalculateButton);
button.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
//执行上的点击动作
}
});
和在标签布局按钮:
<按钮的android:layout_height =WRAP_CONTENT机器人:文本=计算的android:layout_width =WRAP_CONTENT机器人:ID =@ + ID / CalculateButton安卓的onClick =myClickHandler机器人:layout_gravity =中心>< /按钮>
你定义布局的XML文件,一个onclick方法,或者是你在activtity设置OnClickListener?或者两者都是?
的http://developer.android.com/reference/android/view/View.html#attr_android:onClick
好像你可能已经定义的onclick XML属性myClickHandler,但你永远不会实现的方法。
I'm extremely new to Android and Java but this site has helped me until now, so thanks to you all!
I am building an app that has two tabs, in one tab I have created some buttons and in the corresponding activity I have an OnClickListener. When I run the app it forces close and I get the error:
ERROR/AndroidRuntime(25971): java.lang.IllegalStateException: Could not find a method myClickHandler(View) in the activity class com.test.rate.MainActivity for onClick handler on view class android.widget.Button with id 'CalculateButton'
It's probably really simple but help!
Okay yeah I should have added some code:
The Activity:
public class MetricActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.metriclayout);
final Button button = (Button) findViewById(R.id.CalculateButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
And the button in the tab layout:
<Button android:layout_height="wrap_content" android:text="Calculate" android:layout_width="wrap_content" android:id="@+id/CalculateButton" android:onClick="myClickHandler" android:layout_gravity="center"></Button>
Did you define an onclick method in the xml file for the layout, or are you setting the OnClickListener in the activtity? Or both?
http://developer.android.com/reference/android/view/View.html#attr_android:onClick
It seems like you may have defined the onclick xml attribute to myClickHandler but you never implemented the method.
这篇关于我怎么一个按钮分配给Android的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!