마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

간단한 코드인데 에러가 납니다 ㅠ

0 추천
public class MainActivity extends AppCompatActivity {

    EditText editText_id;
    EditText editText_password;


    Socket socket = null;
    ObjectInputStream ois = null;
    ObjectOutputStream oos = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText_id = (EditText) findViewById(R.id.editText_id);
        editText_password = (EditText) findViewById(R.id.editText_password);

    }


    public void onButtonLoginClicked(View v) {  // 연결하기
        ConnectTread thread = new ConnectTread();
        thread.start();
//        send_toServer("Login/"+editText_id.getText().toString()+"@"+
//                editText_password.getText().toString());
        try {
            oos.writeObject(editText_password.getText().toString());
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

//    private void send_toServer(String msg){//Protocol : / 메시지: @
//        try {
//            oos.writeObject(msg);
//            oos.flush();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//    }

    class ConnectTread extends Thread {
        public void run() {
            Myclient client = new Myclient();
            client.connection();
        }
    }

    private class Myclient {

        String ip = "218.151.105.235";
        int port = 5001;

        String msg;

        private void connection() {
            try {
                socket = new Socket(ip, port);
                ois = new ObjectInputStream(socket.getInputStream());
                oos = new ObjectOutputStream(socket.getOutputStream());

            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
try {
    oos.writeObject(editText_password.getText().toString());
    oos.flush();
} catch (IOException e) {
    e.printStackTrace();
}

이부분에서 ObjectOutputStream이 문제가 되네요.. 이와같이 작성한 비슷한 채팅 프로그램은 돌아가는데

왜안되는지 전혀 모르겠습니다 ㅠㅠ 이걸로 한 이틀 밤샌거같네요..

익명사용자 님이 2016년 8월 13일 질문
에러가 났으면 에러로그를 같이 올리세요

1개의 답변

0 추천
시점차 문제네요
아마 요청하신 writeObject함수 부분에서 null포인터 에러가 났을거 같은데
write 하는 부분도 쓰레드 안에 같이 묶어줘야 정상동작하게 될 것입니다.
Development Guy (70,570 포인트) 님이 2016년 8월 15일 답변
...