0
votes

I have a program that logs USB data to a file that I create and it works fine. I am trying to display some of this data using setText but the setText executes once correctly and then the next time it is called I get a Source not found crash.

I am saving the USB data to my file in this Run:

    @Override
public void run() {
    ByteBuffer buffer = ByteBuffer.allocate(64);
    UsbRequest request = new UsbRequest();
    request.initialize(mConnection, mEndpointIntr);
    while (true) {
        request.queue(buffer, 64);
        if (mConnection.requestWait() == request) {
            znum.setText("help"); 
            savetofile("GOT data " + System.currentTimeMillis());
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        } else {
            Log.e(TAG, "requestWait failed, exiting");
            savetofile("10 requestWait failed, exiting");
            break;
        }
    }
}

If I rem out the setText line in the above, the program runs happily, saving my data to a file. When I run the code as above, the program crashes and I get a Source not found indication.

If I start the program with a break on the savetofile line, the layout gets updated with tvZnum set to help just fine.

Then I step thru the code and get back to the setText line.

All is fine until I execute setText the second time, then it crashes.

It seems to be at android.view.ViewRootImpl.checkThread() line 4077 when it crashes. I tried to add a screen capture image to my post here but I don't have enough reputation points yet.

I declare:

    TextView znum;

And have this in OnCreate:

    znum = (TextView) findViewById(R.id.tvZnum);

And tvZnum shows up fine in the launcher and R.

I know the answer must be simple but simple me can't figure it out.

Any ideas out there?

Thanks, Dale

Edit: Here is logCat (Greek to me and I couldn't figure out how to format it here):

I/dalvikvm( 1529): threadid=3: reacting to signal 3

I/dalvikvm( 1529): Wrote stack traces to '/data/anr/traces.txt'

I/System.out( 1529): waiting for debugger to settle...

I/System.out( 1529): waiting for debugger to settle...

I/Process ( 167): Sending signal. PID: 1529 SIG: 3

I/dalvikvm( 1529): threadid=3: reacting to signal 3

I/System.out( 1529): waiting for debugger to settle...

I/dalvikvm( 1529): Wrote stack traces to '/data/anr/traces.txt'

I/System.out( 1529): waiting for debugger to settle...

I/System.out( 1529): waiting for debugger to settle...

I/Process ( 167): Sending signal. PID: 1529 SIG: 3

I/dalvikvm( 1529): threadid=3: reacting to signal 3

I/dalvikvm( 1529): Wrote stack traces to '/data/anr/traces.txt'

I/System.out( 1529): waiting for debugger to settle...

I/System.out( 1529): waiting for debugger to settle...

I/Process ( 167): Sending signal. PID: 1529 SIG: 3

I/dalvikvm( 1529): threadid=3: reacting to signal 3

I/dalvikvm( 1529): Wrote stack traces to '/data/anr/traces.txt'

I/System.out( 1529): waiting for debugger to settle...

I/System.out( 1529): waiting for debugger to settle...

I/System.out( 1529): debugger has settled (1359)

I/Process ( 167): Sending signal. PID: 1529 SIG: 3

I/dalvikvm( 1529): threadid=3: reacting to signal 3

I/dalvikvm( 1529): Wrote stack traces to '/data/anr/traces.txt'

D/dalvikvm( 1529): threadid=1: still suspended after undo (sc=1 dc=1)

D/ProMeasure2DActivity( 1529): intent: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.android.missilelauncher/.MissileLauncherActivity }

D/ProMeasure2DActivity( 1529): setDevice UsbDevice[mName=/dev/bus/usb/001/002,mVendorId=1240,mProductId=63,mClass=0,mSubclass=0,mProtocol=0,mInterfaces=[Landroid.os.Parcelable;@40f9e3b0]

D/ProMeasure2DActivity( 1529): open SUCCESS

D/UsbRequestJNI( 1529): init

D/ViewRootImpl( 1529): pckname = com.android.missilelauncher

I/Process ( 167): Sending signal. PID: 1529 SIG: 3

I/dalvikvm( 1529): threadid=3: reacting to signal 3

D/dalvikvm( 1529): threadid=11: still suspended after undo (sc=1 dc=1)

I/dalvikvm( 1529): Wrote stack traces to '/data/anr/traces.txt'

I/ActivityManager( 167): Displayed com.android.missilelauncher/.MissileLauncherActivity: +3s791ms

I/ActivityManager( 167): No longer want com.android.email (pid 1266): hidden #16

D/Finsky ( 696): [1] 5.onFinished: Installation state replication succeeded.

1
Please post the logcat outputHenry
@Henry Not sure if that is logCat the way you want it. I captured it from PowerCmd as I use adb on tcpip.KiloOne
The views must be updated from the main UI thread, not a background thread(what I'm assuming you're doing). It works the first time because you probably start the thread in the onCreate method and that gives you a small amount of time until the views are actually shown on the screen(and the exception will be thrown). Set the text using runOnUiThread or by posting a Runnable on znum(the post method).user
@Luksprog Thank-you! With that info I figured out a solution (I think). I am not well versed about marking an answer. I don't see a way to mark a comment as an answer so I will post an answer and give you credit if I find a way to do it.KiloOne

1 Answers

1
votes

Based on research I did after I read Luksprog's comment, here is the actual code solution that seems to work:

  1. Remove znum = (TextView) findViewById(R.id.tvZnum); from my OnCreate.
  2. Replace znum.setText("help"); with this code:

            this.runOnUiThread(new Runnable() {
                public void run() {
                    ((TextView) findViewById(R.id.tvZnum)).setText("help");
                }
            });
    

    I believe the result of this is to add the setText action to a queue in the main UI thread.