android instant Error : Unable to instantiate activity ComponentInfo -
i want in this, can't find out problem.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.alnassre.prayingobserver" android:versioncode="2" android:versionname="1.0" > <uses-sdk android:minsdkversion="9" android:targetsdkversion="9" /> <uses-feature android:name="android.hardware.sensor.light" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.alnassre.prayingobserver.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
and
package com.alnassre.prayingobserver; import android.hardware.sensor; import android.hardware.sensorevent; import android.hardware.sensoreventlistener; import android.hardware.sensormanager; import android.os.bundle; import android.app.activity; import android.util.log; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.textview; import android.widget.toast; public class mainactivity extends activity implements sensoreventlistener { private sensormanager m_sensor_manager; private sensor m_accelerometer; final button button = (button) findviewbyid(r.id.btn1); final textview txt_rkoa = (textview) findviewbyid(r.id.textview1); final textview txt_sjod = (textview) findviewbyid(r.id.textview2); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); m_sensor_manager = (sensormanager) getsystemservice(sensor_service); m_accelerometer = m_sensor_manager.getdefaultsensor(sensor.type_light); setcontentview(r.layout.activity_main); button.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { int tkoa = integer.parseint(txt_rkoa.gettext().tostring()); tkoa++; txt_rkoa.settext(string.valueof(tkoa)); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_main, menu); return true; } protected void onresume() { super.onresume(); m_sensor_manager.registerlistener(this, m_accelerometer, sensormanager.sensor_delay_normal); } public void onsensorchanged(sensorevent event) { _("onsensorchanged"); _("" + event.values[0]); // toast.maketext(this, "onsensorchanged "+ event.values[0], toast.length_short).show(); log.i("onsensorchanged ", event.values[0]+""); } public void onaccuracychanged(sensor sensor, int accuracy) { toast.maketext(this, "onaccuracychanged", toast.length_short).show(); } private void _(string msg) { log.d("chovanec", msg); } }
error statement:
05-19 22:13:24.664: e/androidruntime(12565): java.lang.runtimeexception: unable instantiate activity componentinfo{com.alnassre.prayingobserver/com.alnassre.prayingobserver.mainactivity}: java.lang.nullpointerexception
your error at:
final button button = (button) findviewbyid(r.id.btn1); final textview txt_rkoa = (textview) findviewbyid(r.id.textview1); final textview txt_sjod = (textview) findviewbyid(r.id.textview2);
you trying find views before oncreate called set content setcontentview(r.layout.activity_main);
should more like:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); m_sensor_manager = (sensormanager) getsystemservice(sensor_service); m_accelerometer = m_sensor_manager.getdefaultsensor(sensor.type_light); setcontentview(r.layout.activity_main); button = (button) findviewbyid(r.id.btn1); txt_rkoa = (textview) findviewbyid(r.id.textview1); txt_sjod = (textview) findviewbyid(r.id.textview2); button.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { int tkoa = integer.parseint(txt_rkoa.gettext().tostring()); tkoa++; txt_rkoa.settext(string.valueof(tkoa)); } }); }
also in future, past entire trace stack tell failing @ (nullpointerexception)
Comments
Post a Comment