1

我正在通过以太网向安卓应用程序发送模拟信号。目标是控制正在绘制的某些形状的 Alpha 通道。该代码专门适用于矩形。我已经让两个教程分开工作。通讯器接收到信号并将值打印到屏幕上。图形绘制一个矩形并根据触摸事件在屏幕上移动它。

NullExceptionErrorsocket = 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
}
4

2 回答 2

0

看着:

try {
    serverSocket = new ServerSocket(SERVERPORT);
} catch (IOException e) {
    e.printStackTrace(); 
}

当服务器套接字创建失败(即端口已被使用)时,您只需启动堆栈跟踪并继续下一行,就好像创建了套接字一样。

于 2013-11-03T08:55:12.180 回答
0

它在socket = serverSocket.accept() line.

实际上并没有。它抛出一个 NullPointerException,这意味着它serverSocket必须为 null。

这不会单独发生。

不管什么意思。

while (!Thread.currentThread().isInterrupted() && socket==null) {

测试的重点socket == null让我忘记了。它所完成的只是确保您一次只处理一个连接。这真的是你的意图吗?如果是这样,为什么?

于 2013-11-03T08:50:59.347 回答