android - Error inflating class button -
i started learning computer science , android development. i've been going through helloworld demos try , learn.
so going wild, tried rewrite program has 1 button 2 buttons using onclicklistener. while don't have compiling errors, program force closing on me:
01-05 11:20:33.968: e/androidruntime(3257): java.lang.runtimeexception: unable start activity componentinfo{com.example.multbuttontest/com.example.multbuttontest.multibuttonactivity}: android.view.inflateexception: binary xml file line #16: error inflating class button
my xml file looks (sorry suck @ formatting):
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".multibuttonactivity"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" />
and code:
package com.example.multbuttontest; import android.os.bundle; import android.app.activity; import android.view.view; import android.widget.button; public class multibuttonactivity extends activity implements view.onclicklistener{ button button1, button2; int touchcount1, touchcount2; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_multi_button); button1 = (button) findviewbyid(r.id.button1); button1.settext( "touch button 1!"); button1.setonclicklistener(this); button2 = (button) findviewbyid(r.id.button2); button2.settext( "touch button 2!"); button2.setonclicklistener(this); } public void onclick(view v){ switch(v.getid()){ case r.id.button1: touchcount1++; button1.settext("touched b1 " + touchcount1 + " times(s)"); break; case r.id.button2: touchcount2++; button2.settext("touched b2 " + touchcount2 + " times(s)"); break; } } }
this strictly learning purposes, , code suck. appreciated.
here code might out.
mainactivity:
public class mainactivity extends activity { private textview txt1; private button btncount,btnclear; private int num1 = 1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); txt1 = (textview) findviewbyid(r.id.txt1); btncount = (button) findviewbyid(r.id.button1); btnclear = (button) findviewbyid(r.id.button2); } public void sendmessage(view view) { txt1.settext("you have clicked"+ (num1) + "times"); num1++; } public void clear(view view) { num1 = 1; txt1.settext("you have clicked"+ (num1) + "times"); } }
activity_main.xml:
<button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_send" android:onclick="sendmessage" /> <button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_clear" android:onclick="clear" /> <textview android:id="@+id/txt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@+string/pushed" />
output:
Comments
Post a Comment