java - onResume stops my application -
my application gets serial usb input ftdi chip, managed it. press button 3 input float type numbers displayed on app. here onclick
:
public void onreadclick(view view) throws unsupportedencodingexception { // [ftdriver] create read buffer byte[] rbuf = new byte[27]; // 1byte <--slow-- [transfer speed] --fast--> 4096 byte // [ftdriver] read usb serial len = mserial.read(rbuf); for(i=0; i<len; i++) { mtext1.append((char) rbuf[i]); } text = mtext1.tostring(); string[] splited = text.split("#"); long r = long.parselong(splited[1], 16); float f = float.intbitstofloat(r.intvalue()); d1 = f.tostring(); val.settext(d1); long r2 = long.parselong(splited[2], 16); float f2 = float.intbitstofloat(r2.intvalue()); d2 = f2.tostring(); val2.settext(d2); long r3 = long.parselong(splited[3], 16); float f3 = float.intbitstofloat(r3.intvalue()); d3 = f3.tostring(); val3.settext(d3); monitor.settext(splited[1]); monitor2.settext(splited[2]); monitor3.settext(splited[3]); }
it until wanted make application steps outside of button onclick
, take outside (i'm new java) :
package com.application.i; import java.io.unsupportedencodingexception; import jp.ksksue.driver.serial.ftdriver; import android.app.activity; import android.app.pendingintent; import android.content.context; import android.content.intent; import android.hardware.usb.usbmanager; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.textview; import android.widget.toast; public class ftdi extends activity{ // [ftdriver] object ftdriver mserial; // [ftdriver] permission string private static final string action_usb_permission = "jp.ksksue.tutorial.usb_permission"; button btnread; textview monitor,monitor2,monitor3; textview val,val2,val3; textview indicator; stringbuffer mtext1 = new stringbuffer(); int i,len; string text=null; boolean doit = true; string number=null,number2=null,number3=null, hadar1=null, hadar2=null, hadar3=null, d1=null, d2=null, d3=null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.ftdisub); btnread = (button) findviewbyid(r.id.btnread); monitor = (textview)findviewbyid(r.id.onitor); monitor2 = (textview)findviewbyid(r.id.onitor2); monitor3 = (textview)findviewbyid(r.id.onitor3); val = (textview)findviewbyid(r.id.dec); val2 = (textview)findviewbyid(r.id.dec2); val3 = (textview)findviewbyid(r.id.dec3); indicator = (textview)findviewbyid(r.id.indicator); // [ftdriver] create instance mserial = new ftdriver((usbmanager)getsystemservice(context.usb_service)); // [ftdriver] setpermissionintent() before begin() pendingintent permissionintent = pendingintent.getbroadcast(this, 0, new intent(action_usb_permission), 0); mserial.setpermissionintent(permissionintent); // [ftdriver] open usb serial if(mserial.begin(ftdriver.baud115200)) { btnread.setenabled(true); toast.maketext(this, "connected", toast.length_short).show(); } else { toast.maketext(this, "cannot connect", toast.length_short).show(); } } @override protected void onresume() { if (doit==true){ // [ftdriver] create read buffer byte[] rbuf = new byte[27]; // 1byte <--slow-- [transfer speed] --fast--> 4096 byte // [ftdriver] read usb serial len = mserial.read(rbuf); for(i=0; i<len; i++) { mtext1.append((char) rbuf[i]); } text = mtext1.tostring(); string[] splited = text.split("#"); long r = long.parselong(splited[1], 16); float f = float.intbitstofloat(r.intvalue()); d1 = f.tostring(); val.settext(d1); long r2 = long.parselong(splited[2], 16); float f2 = float.intbitstofloat(r2.intvalue()); d2 = f2.tostring(); val2.settext(d2); long r3 = long.parselong(splited[3], 16); float f3 = float.intbitstofloat(r3.intvalue()); d3 = f3.tostring(); val3.settext(d3); monitor.settext(splited[1]); monitor2.settext(splited[2]); monitor3.settext(splited[3]); doit=false; } }; @override public void ondestroy() { super.ondestroy(); // [ftdriver] close usb serial mserial.end(); } public void onreadclick(view view) throws unsupportedencodingexception { doit=true; } }
i want read once without button
, , each additive time next, press button application process again. main thing want outside of button, once ftdi activity being activated.
the ftdi activity activated click button on application main screen.
problem: when click ftdi button on main screen should start ftdi activity , display 3 float numbers, instead application finished error , close message.
logcat:
05-15 11:04:46.855: e/androidruntime(9598): fatal exception: main 05-15 11:04:46.855: e/androidruntime(9598): java.lang.runtimeexception: unable resume activity {com.application.i/com.application.i.ftdi}: java.lang.nullpointerexception 05-15 11:04:46.855: e/androidruntime(9598): @ android.app.activitythread.performresumeactivity(activitythread.java:2616) 05-15 11:04:46.855: e/androidruntime(9598): @ android.app.activitythread.handleresumeactivity(activitythread.java:2644) 05-15 11:04:46.855: e/androidruntime(9598): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2130) 05-15 11:04:46.855: e/androidruntime(9598): @ android.app.activitythread.access$600(activitythread.java:140) 05-15 11:04:46.855: e/androidruntime(9598): @ android.app.activitythread$h.handlemessage(activitythread.java:1227) 05-15 11:04:46.855: e/androidruntime(9598): @ android.os.handler.dispatchmessage(handler.java:99) 05-15 11:04:46.855: e/androidruntime(9598): @ android.os.looper.loop(looper.java:137) 05-15 11:04:46.855: e/androidruntime(9598): @ android.app.activitythread.main(activitythread.java:4898) 05-15 11:04:46.855: e/androidruntime(9598): @ java.lang.reflect.method.invokenative(native method) 05-15 11:04:46.855: e/androidruntime(9598): @ java.lang.reflect.method.invoke(method.java:511) 05-15 11:04:46.855: e/androidruntime(9598): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1006) 05-15 11:04:46.855: e/androidruntime(9598): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:773) 05-15 11:04:46.855: e/androidruntime(9598): @ dalvik.system.nativestart.main(native method) 05-15 11:04:46.855: e/androidruntime(9598): caused by: java.lang.nullpointerexception 05-15 11:04:46.855: e/androidruntime(9598): @ jp.ksksue.driver.serial.ftdriver.read(ftdriver.java:271) 05-15 11:04:46.855: e/androidruntime(9598): @ jp.ksksue.driver.serial.ftdriver.read(ftdriver.java:257) 05-15 11:04:46.855: e/androidruntime(9598): @ com.application.i.ftdi.onresume(ftdi.java:83) 05-15 11:04:46.855: e/androidruntime(9598): @ android.app.instrumentation.callactivityonresume(instrumentation.java:1188) 05-15 11:04:46.855: e/androidruntime(9598): @ android.app.activity.performresume(activity.java:5280) 05-15 11:04:46.855: e/androidruntime(9598): @ android.app.activitythread.performresumeactivity(activitythread.java:2606) 05-15 11:04:46.855: e/androidruntime(9598): ... 12 more
p.s : ftdi activity works fine:
public class ftdi extends activity{ // [ftdriver] object ftdriver mserial; // [ftdriver] permission string private static final string action_usb_permission = "jp.ksksue.tutorial.usb_permission"; button btnread; textview monitor,monitor2,monitor3; textview val,val2,val3; textview indicator; stringbuffer mtext1 = new stringbuffer(); int i,len; string text=null; boolean doit = true; string number=null,number2=null,number3=null, hadar1=null, hadar2=null, hadar3=null, d1=null, d2=null, d3=null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.ftdisub); btnread = (button) findviewbyid(r.id.btnread); monitor = (textview)findviewbyid(r.id.onitor); monitor2 = (textview)findviewbyid(r.id.onitor2); monitor3 = (textview)findviewbyid(r.id.onitor3); val = (textview)findviewbyid(r.id.dec); val2 = (textview)findviewbyid(r.id.dec2); val3 = (textview)findviewbyid(r.id.dec3); indicator = (textview)findviewbyid(r.id.indicator); // [ftdriver] create instance mserial = new ftdriver((usbmanager)getsystemservice(context.usb_service)); // [ftdriver] setpermissionintent() before begin() pendingintent permissionintent = pendingintent.getbroadcast(this, 0, new intent(action_usb_permission), 0); mserial.setpermissionintent(permissionintent); // [ftdriver] open usb serial if(mserial.begin(ftdriver.baud115200)) { btnread.setenabled(true); toast.maketext(this, "connected", toast.length_short).show(); } else { toast.maketext(this, "cannot connect", toast.length_short).show(); } } @override public void ondestroy() { super.ondestroy(); // [ftdriver] close usb serial mserial.end(); } public void onreadclick(view view) throws unsupportedencodingexception { // [ftdriver] create read buffer byte[] rbuf = new byte[27]; // 1byte <--slow-- [transfer speed] --fast--> 4096 byte // [ftdriver] read usb serial len = mserial.read(rbuf); for(i=0; i<len; i++) { mtext1.append((char) rbuf[i]); } text = mtext1.tostring(); string[] splited = text.split("#"); long r = long.parselong(splited[1], 16); float f = float.intbitstofloat(r.intvalue()); d1 = f.tostring(); val.settext(d1); long r2 = long.parselong(splited[2], 16); float f2 = float.intbitstofloat(r2.intvalue()); d2 = f2.tostring(); val2.settext(d2); long r3 = long.parselong(splited[3], 16); float f3 = float.intbitstofloat(r3.intvalue()); d3 = f3.tostring(); val3.settext(d3); monitor.settext(splited[1]); monitor2.settext(splited[2]); monitor3.settext(splited[3]); } }
you have check life cycle of activity. guess mserial not initialized when on resume method called , try stuff in on start
Comments
Post a Comment