Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7915973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:38:12+00:00 2026-06-03T14:38:12+00:00

I am facing a problem that I failed to start an activity from a

  • 0

I am facing a problem that I failed to start an activity from a service. When the service is receiving data, it calls handler.sendEmptyMessage(0) to send handler message. The receiver then starts the new activity. But now the activity cannot get started. Could someone read the code below and give me some suggestions?

package com.testBlueTooth;

import java.io.InputStream;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.provider.SyncStateContract.Constants;
import android.util.Log;
import android.view.SurfaceView;
import android.widget.Toast;

public class ClsWaveDiagram extends Service{

private boolean isRecording = false;
private InputStream btInput = null;

public int rateX = 1;

public int rateY = 1;

public int baseLine = 0;

public ClsWaveDiagram(InputStream btInput, int rateX, int rateY,
        int baseLine) {
    this.btInput = btInput;
    this.rateX = rateX;
    this.rateY = rateY;
    this.baseLine = baseLine;
}


public void Start(SurfaceView sfv, Paint mPaint, int wait) {
    isRecording = true;
    new DrawThread(sfv, mPaint, wait).start();
}

public void Stop() {
    isRecording = false;
}


class DrawThread extends Thread {

    private int oldX = 0;
    private int oldY = 0;
    private SurfaceView sfv;
    private int X_index = 0;
    private Paint mPaint;
    private int wait = 50;

    public DrawThread(SurfaceView sfv, Paint mPaint, int wait) {
        this.sfv = sfv;
        this.mPaint = mPaint;
        this.wait = wait;
    }

    public void run() {
        while (isRecording) {
            try {

                byte[] temp = new byte[1024];

                int len = btInput.read(temp);
                Log.e("available", String.valueOf(len));

                System.out.println("alarm!");

                handler.sendEmptyMessage(0);

                if (len > 0) {
                    byte[] btBuf = new byte[len];
                    System.arraycopy(temp, 0, btBuf, 0, btBuf.length);
                    SimpleDraw(X_index, btBuf, rateX, rateY, baseLine);
                    X_index = X_index + (btBuf.length/rateX) - 1;
                    if (X_index > sfv.getHeight()) {
                        X_index = 0;
                    }
                }
                Thread.sleep(wait);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }   



    void SimpleDraw(int start, byte[] inputBuf, int rateX, int rateY,
            int baseLine) {
        if (start == 0)
            oldX = 0;

        byte[] buffer = new byte[inputBuf.length / rateX];
        for (int i = 0, ii = 0; i < buffer.length; i++, ii = i * rateX)
            buffer[i] = inputBuf[ii];

        Canvas canvas = sfv.getHolder().lockCanvas(
                new Rect(0, start, sfv.getWidth(), start + buffer.length));
        canvas.drawColor(Color.BLACK);

        for (int i = 0; i < buffer.length; i++) {
            int y = i + start;
            int x = (0xFF - (buffer[i] & 0xFF))

                    / rateY + baseLine;
            canvas.drawLine(oldX, oldY, x, y, mPaint);
            oldX = x;
            oldY = y;
        }
        sfv.getHolder().unlockCanvasAndPost(canvas);
    }
}
private Handler handler = new Handler(){
    public void handleMessage(android.os.Message msg) {
        Intent intent = new Intent();


        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        intent.setClass(ClsWaveDiagram.this.getApplicationContext(), AlertActivity.class);


        ClsWaveDiagram.this.startActivity(intent);

    };};

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

}

The logcat errors is below:

04-16 12:34:50.559: D/AndroidRuntime(3706): Shutting down VM
04-16 12:34:50.559: W/dalvikvm(3706): threadid=3: thread exiting with uncaught     exception (group=0x4001b170)
04-16 12:34:50.559: E/AndroidRuntime(3706): Uncaught handler: thread main exiting due     to uncaught exception
04-16 12:34:50.559: E/AndroidRuntime(3706): java.lang.NullPointerException
04-16 12:34:50.559: E/AndroidRuntime(3706):     at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
04-16 12:34:50.559: E/AndroidRuntime(3706):     at com.testBlueTooth.ClsWaveDiagram$1.handleMessage(ClsWaveDiagram.java:199)
04-16 12:34:50.559: E/AndroidRuntime(3706):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 12:34:50.559: E/AndroidRuntime(3706):     at android.os.Looper.loop(Looper.java:123)
04-16 12:34:50.559: E/AndroidRuntime(3706):     at android.app.ActivityThread.main(ActivityThread.java:4363)
04-16 12:34:50.559: E/AndroidRuntime(3706):     at java.lang.reflect.Method.invokeNative(Native Method)
04-16 12:34:50.559: E/AndroidRuntime(3706):     at java.lang.reflect.Method.invoke(Method.java:521)
04-16 12:34:50.559: E/AndroidRuntime(3706):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-16 12:34:50.559: E/AndroidRuntime(3706):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-16 12:34:50.559: E/AndroidRuntime(3706):     at dalvik.system.NativeStart.main(Native Method)
04-16 12:34:50.590: I/dalvikvm(3706): threadid=7: reacting to signal 3
04-16 12:34:50.614: I/dalvikvm(3706): Wrote stack trace to '/data/anr/traces.txt'
04-16 12:34:54.789: E/available(3706): 2
04-16 12:34:54.789: I/System.out(3706): alarm!
04-16 12:34:54.903: E/available(3706): 20
04-16 12:34:54.903: I/System.out(3706): alarm!
04-16 12:34:59.034: E/available(3706): 1
04-16 12:34:59.034: I/System.out(3706): alarm!

Main Activity:

package com.testBlueTooth;

import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

public class WaveDiagram extends Activity {
ClsWaveDiagram clsWaveDiagram;
SurfaceView sfvWave;
Paint mPaint;
InputStream btInput;
Context context = WaveDiagram.this;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wavediagram);

    sfvWave = (SurfaceView) this.findViewById(R.id.sfvWave);
    sfvWave.setOnTouchListener(new TouchEvent());

    mPaint = new Paint();
    mPaint.setColor(Color.GREEN);
    mPaint.setStrokeWidth(2);

    try {
        if (testBlueTooth.btSocket.getInputStream() != null) {
            clsWaveDiagram = new ClsWaveDiagram(
                    testBlueTooth.btSocket.getInputStream(),
                    3,
                    1,
                    sfvWave.getWidth()/2);

            clsWaveDiagram.Start(sfvWave, mPaint,80);
            Log.e("clsWaveDiagram","start");

        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

@Override
public void onDestroy()  
{  
    clsWaveDiagram.Stop();
    try {
        testBlueTooth.btSocket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    super.onDestroy();  
}
class TouchEvent implements OnTouchListener {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        clsWaveDiagram.baseLine = (int) event.getX();
        return true;
    }

}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:38:15+00:00Added an answer on June 3, 2026 at 2:38 pm

    Don’t run DrawThread before Service.onStartCommand()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am facing one problem regarding unlocking the device when Service launched activity when
I'm facing a problem that seems to have no straighforward solution. I'm using java.util.Map
I am facing the problem that I cannot properly map my foreign key table
Here i am facing a problem that , i configured the hibernate sessionfactory in
I'm working with Eclipse and ClearCase and we're facing the problem that there's no
I am new to TCPDF. The small problem that I am facing is that
I'm facing a problem while deleting a record that is referenced by another table.
I am new to Python and Numpy, and I am facing a problem, that
Actually, I am facing a problem in Android. I am creating an application that
I'm currently facing the problem that I want to serve static content via a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.