我正在通过以太网向安卓应用程序发送模拟信号。目标是控制正在绘制的某些形状的 Alpha 通道。该代码专门适用于矩形。我已经让两个教程分开工作。通讯器接收到信号并将值打印到屏幕上。图形绘制一个矩形并根据触摸事件在屏幕上移动它。
它NullExceptionError
上socket = serverSocket.accept()
线了。这不会单独发生。
我已经指出哪一部分是图形教程的一部分,哪一部分是通信教程。像 onCreate 这样的部分是组合在一起的。
谢谢
public class MainActivity extends Activity implements OnTouchListener {
DrawView v;
float x;
float y;
Bitmap pic;
int alpha;
private ServerSocket serverSocket;
Handler updateConversationHandler;
Thread serverThread = null;
public static final int SERVERPORT = 6000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
v.setOnTouchListener(this);
setContentView(v);
updateConversationHandler = new Handler();
this.serverThread = new Thread(new ServerThread());
this.serverThread.start();
}
@Override
protected void onStop() {
super.onStop();
try {
serverSocket.close();
finish();
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
}
//communications tutorial
class ServerThread implements Runnable {
public void run() {
Socket socket = null;
try {
serverSocket = new ServerSocket(SERVERPORT);
} catch (IOException e) {
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted() && socket==null) {
try {
socket = serverSocket.accept(); //null exception here
CommunicationThread commThread = new CommunicationThread(socket);
new Thread(commThread).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class CommunicationThread implements Runnable {
private Socket clientSocket;
private BufferedReader input;
public CommunicationThread(Socket clientSocket) {
this.clientSocket = clientSocket;
try {
this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
String read = input.readLine();
int value = Integer.parseInt(read);
alpha=value*(255/1023);
//updateConversationHandler.post(new updateUIThread(read));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//end communications tutorial
//graphics tutorial
private void init(){
v = new DrawView(this);
//pic = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
x = 0;
y = 0;
alpha=255;
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
v.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
v.resume();
}
public class DrawView extends SurfaceView implements Runnable{
Thread t = null;
SurfaceHolder holder;
boolean running = false;
public DrawView(Context c){
super(c);
holder = getHolder();
}
public void run() {
while(running){
if(holder.getSurface().isValid()){
Canvas c = holder.lockCanvas();
c.drawARGB(255, 0, 0, 0);
//c.drawBitmap(pic, x-pic.getWidth(), y-pic.getHeight(), null);
Paint myPaint = new Paint();
myPaint.setColor(Color.rgb(255, 255, 255));
myPaint.setAlpha(alpha);
// TODO implement alpha //myPaint.setAlpha(a);
c.drawRect(x-100,y-100,x+100,y+100,myPaint);
holder.unlockCanvasAndPost(c);
}
}
}
public void pause(){
running = false;
while(true){
try{
t.join();
}catch(InterruptedException e){
e.printStackTrace();
}break;
}
t = null;
}
public void resume(){
running = true;
t = new Thread(this);
t.start();
}
}
public boolean onTouch(View v, MotionEvent me) {
x = me.getX();
y = me.getY();
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event){
if((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_MOVE) {
if(event.getPointerCount() == 2) {
x = event.getX(0);
y = event.getY(0);
}
}
return true;
}
//end graphics tutorial
}