我面临一个问题,我无法从服务启动活动。当服务接收数据时,它会调用handler.sendEmptyMessage(0)
发送处理程序消息。然后接收者开始新的活动。但现在活动无法开始。有人可以阅读下面的代码并给我一些建议吗?
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;
}
}
logcat 错误如下:
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!
主要活动:
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;
}
}
}