1

i am making a android app that connects to my chat server. When I start the app and press the Join button it stops I don't know what the problem is. If you can help me thank you in advance.

package com.example.marcus.chatclient1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class chat extends AppCompatActivity {
public Socket s;

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

    final Button join = (Button)findViewById(R.id.joinButton);
    join.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            EditText nameText = (EditText)findViewById(R.id.editTextName);
            String name = nameText.getText().toString();
            join.setVisibility(View.INVISIBLE);
            nameText.setVisibility(View.INVISIBLE);
            TextView errorT = (TextView) findViewById(R.id.errorText);
            try {
                s = new Socket("192.168.0.15", 55555);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
}
}
Marcus Mardis
  • 83
  • 1
  • 1
  • 8
  • Post the stacktrace of the exception. – Gil Vegliach Dec 13 '15 at 00:41
  • i added e.printStackTrace(); but it doesn't show an exception it just says "Unfortunately, ChatClient1 has stopped" – Marcus Mardis Dec 13 '15 at 00:48
  • You catch the IOException, so it won't crash there. You probably have a NullPointerException somewhere, check the logs and then copy it here. – Gil Vegliach Dec 13 '15 at 00:49
  • i am catching the IOException but it still just crashes and doesn't says "Unfortunately, ChatClient1 has stopped" it doesn't show an error – Marcus Mardis Dec 13 '15 at 00:55
  • i updated the code and took out the ObjectOutputStream and i am just trying to connect but it still doesn't work. – Marcus Mardis Dec 13 '15 at 00:56
  • You wouldn't happen to do [network IO on the UI thread](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception), right? – tilpner Dec 13 '15 at 01:03

1 Answers1

0

Basically there are answers here

You problem it that you are trying to create socket on a main thread.

Community
  • 1
  • 1
lganzzzo
  • 558
  • 3
  • 10