java - Eclipse error : button1 cannot be resolved or is not a field -
here mainactivity.java code :
package com.dg.buttontest3; import android.os.bundle; import android.app.activity; import android.util.log; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; public class mainactivity extends activity implements onclicklistener{ button button; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button = (button)findviewbyid(r.id.button1); button.setonclicklistener(this); } @override public void onclick(view v) { // todo auto-generated method stub log.d("dg","button clicked"); } }
and here activity_main.xml code :
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_marginleft="18dp" android:layout_margintop="18dp" android:text="button" /> </linearlayout>
i getting following error while running code on eclipse adt : button1 cannot resolved or not field. don't understand wrong code. please help.
you haven't posted xml code, code seems working properly:
button = (button)findviewbyid(r.id.button1);
i'd guess in activity_main.xml
never create called button1
, or don't use @+id/
prefix.
it should like:
<button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1"/>
just realised else, need change linearlayout
opening tag has >
.
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
that might cause button never recognised.
Comments
Post a Comment