Reputation: 352
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();
}
Upvotes: 1
Views: 33
Reputation: 157487
move setContentView(R.layout.activity_main);
outside your anonymous-inner thread class, and get rid of setContentView(R.layout.promptname);
You need to switch between views, you should look into Fragment
Upvotes: 1