java - "Unfortunately <myApp> has Stopeed" after running for 45sec on Nexus4 -
i'm beginner in android app development. using eclipse develop simple bluetooth shaker app. tried on phone , runs fine 30 seconds. , closes saying "unfortunately theapp has stopped". app supposed switch on bluetooth shake , play beep sound(pop.mp3)
please help.
main.java>
public class blueactivity extends activity implements sensorlistener, oncompletionlistener { button a, b,d; edittext e; textview f; protected final int id=1; bluetoothadapter bt; public mediaplayer m; private sensormanager sensormgr; private long lastupdate = -1; private float x, y, z; private float last_x, last_y, last_z; private static final int shake_threshold = 3000; @override protected void oncreate(bundle savedinstancestate) { requestwindowfeature(window.feature_no_title); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_blue); a=(button)findviewbyid(r.id.button1); b=(button)findviewbyid(r.id.button2); d=(button)findviewbyid(r.id.button3); f=(textview)findviewbyid(r.id.textview1); e=(edittext)findviewbyid(r.id.edittext1); e.setfocusable(false); b.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { // todo auto-generated method stub string s = bt.getaddress(); e.settext(s); } }); a.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { // todo auto-generated method stub intent = new intent(bluetoothadapter.action_request_discoverable); startactivityforresult(i, id); } }); d.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { // todo auto-generated method stub uri uri = uri.parse("http://www.gamerspitch.com"); intent in = new intent(intent.action_view, uri); startactivity(in); } }); sensormgr = (sensormanager) getsystemservice(sensor_service); boolean accelsupported sensormgr.registerlistener(this,sensormanager.sensor_accelerometer, sensormanager.sensor_delay_normal); bt= bluetoothadapter.getdefaultadapter(); if (!accelsupported) { // on accelerometer on device sensormgr.unregisterlistener(this,sensormanager.sensor_accelerometer); } } protected void onpause() { m = mediaplayer.create(this, r.raw.pop); m.setoncompletionlistener(this); if (sensormgr != null) { sensormgr.registerlistener(this,sensormanager.sensor_accelerometer); sensormgr = null; } if(bt.isenabled()) m.start(); super.onpause(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.blue, menu); return true; } @override public void onaccuracychanged(int sensor, int accuracy) { // todo auto-generated method stub } @override public void onsensorchanged(int sensor, float[] values) { // todo auto-generated method stub if (sensor == sensormanager.sensor_accelerometer) { long curtime = system.currenttimemillis(); // allow 1 update every 100ms. if ((curtime - lastupdate)> 100) { long difftime = (curtime - lastupdate); lastupdate = curtime; m = mediaplayer.create(this, r.raw.pop); m.setoncompletionlistener(this); x = values[sensormanager.data_x]; y = values[sensormanager.data_y]; z = values[sensormanager.data_z]; float speed=math.abs(x+y+z - last_x - last_y - last_z)/ difftime*10000; if (speed > shake_threshold) { // yes, shake action! it! //toast.maketext(this, "shaking", toast.length_short).show(); if(!bt.isenabled()) { bt.enable(); f.settext("on"); m.start(); } else { bt.disable(); f.settext("off"); } } last_x = x; last_y = y; last_z = z; } } } @override public void oncompletion(mediaplayer mp) { // todo auto-generated method stub } }
does have exception handling? because i'm bad @ that.
manifext.xml >
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.gamerspitch.easybluetooth" android:anydensity="false" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="17" /> <uses-permission android:name="android.permission.bluetooth_admin" /> <uses-permission android:name="android.permission.bluetooth" /> <application android:allowbackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.gamerspitch.easybluetooth.splashscreen" android:label="@string/title_activity_splash_screen" android:screenorientation="portrait"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name="com.gamerspitch.easybluetooth.blueactivity" android:label="@string/app_name" android:screenorientation="portrait" > <intent-filter> <action android:name="android.intent.action.view" /> <category android:name="android.intent.category.default" /> </intent-filter> </activity> </application> </manifest>
the logcat >
`08-08 16:03:33.112: e/sensormanager(4428): exception dispatching input event. 08-08 16:03:33.112: e/androidruntime(4428): fatal exception: main 08-08 16:03:33.112: e/androidruntime(4428): java.lang.nullpointerexception 08-08 16:03:33.112: e/androidruntime(4428): @ com.gamerspitch.easybluetooth.blueactivity.onsensorchanged(blueactivity.java:163) 08-08 16:03:33.112: e/androidruntime(4428): @ android.hardware.legacysensormanager$legacylistener.onsensorchanged(legacysensormanager.jav a:274) 08-08 16:03:33.112: e/androidruntime(4428): @ android.hardware.systemsensormanager$sensoreventqueue.dispatchsensorevent(systemsensormanager.java:371) 08-08 16:03:33.112: e/androidruntime(4428): @ android.os.messagequeue.nativepollonce(native method) 08-08 16:03:33.112: e/androidruntime(4428): @ android.os.messagequeue.next(messagequeue.java:132) 08-08 16:03:33.112: e/androidruntime(4428): @ android.os.looper.loop(looper.java:124) 08-08 16:03:33.112: e/androidruntime(4428): @ android.app.activitythread.main(activitythread.java:5103) 08-08 16:03:33.112: e/androidruntime(4428): @ java.lang.reflect.method.invokenative(native method) 08-08 16:03:33.112: e/androidruntime(4428): @ java.lang.reflect.method.invoke(method.java:525) 08-08 16:03:33.112: e/androidruntime(4428): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 08-08 16:03:33.112: e/androidruntime(4428): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 08-08 16:03:33.112: e/androidruntime(4428): @ dalvik.system.nativestart.main(native method)
`
thank in advance.
it's hard null without knowing line 163, guess there has been issue while creating mediaplayer object here:
m = mediaplayer.create(this, r.raw.pop); m.setoncompletionlistener(this);
as documentation create states: mediaplayer object, or null if creation failed
Comments
Post a Comment