lundi 11 mai 2015

error on switching layout in android

I'm trying to switch the layout but the error below keeps coming up.

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.setId(int)' on a null object reference

android studio is complaining on "setContentView(R.layout.activity_main);"

I'm not sure how to fix this error. I googled just the error and tried several of the fixes but none worked with the thread for networking added. I'm probably just missing something simple. Any help is much appreciated.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    new Thread(new Runnable() {
        public void run() {
            try {
                new ChatServer();
            } catch (IOException e) {
                e.printStackTrace();
            }
            setContentView(R.layout.activity_main);
            client = new Client();
            client.start();
            Network.register(client);
            client.addListener(new Listener() {
                public void connected(Connection connection) {
                    Network.RegisterName registerName = new Network.RegisterName();
                    registerName.name = name;
                    client.sendTCP(registerName);
                }

                public void received(Connection connection, Object object) {
                    if (object instanceof Network.ChatMessage) {
                        Network.ChatMessage chatMessage = (Network.ChatMessage) object;
                        System.out.println(chatMessage.text);
                        return;
                    }
                }
            });
            host = client.discoverHosts(54777, 5000);
            while (host == null) {
                host = client.discoverHosts(54777, 5000);
                System.out.println(host);
            }
            if (host.isEmpty()) {
                addmsg("no hosts available");
                System.exit(1);
            }
            final InetAddress address = host.get(0);
            setContentView(R.layout.promptname);
        }
    }).start();




}

Aucun commentaire:

Enregistrer un commentaire