Starting an activity as a Thread?
<p>I'm currently struggling to create a mobile (Android) version of a "battleship" program I recently coded. While my GUI is finished by now and most parts of the program should work, I have (at least) one main problem left: the main part of my program besides the server part (which will react to user input, etc.) relies on Java NIO, i.e. network access. However, since it's running on the main thread, I receive</p>

<pre><code>java.lang.RuntimeException: Unable to start activity ComponentInfo{x.battleship/x.battleship.BattleshipMain}: android.os.NetworkOnMainThreadException`
</code></pre>

<p>(I replaced the package name with x for privacy reasons).</p>

<p>Hence, my question is: how can I start an activity as a new Thread? so far, i have used</p>

<pre><code>Intent i = new Intent(getApplicationContext(), BattleshipMain.class);
startActivity(i);
i.putExtra("ip",joinip);
i.putExtra("port",joinport);
</code></pre>

<p>at first, i thought this would be an alternative:</p>

<pre><code>new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Intent i = new Intent(getApplicationContext(), BattleshipMain.class);
                            startActivity(i);
                            i.putExtra("ip",joinip);
                            i.putExtra("port",joinport);
                        }
                    });
</code></pre>

<p>However, it doesn't seem to work - I get a message in the console, that <code>BattleshipMain</code> has been started, but nothing actually shows up on the display (except whatever was there already).
does anybody have an idea what to do? I really don't know how to solve this; I'm still new to Android, though, and not really a long-term expert in java either.</p>

<p>Thanks in advance!</p>

<p>//edit: I'm not too familiar if I'm supposed to do this, but after I moved the networking code to a separate thread, it seems to me that I'm unable to connect the app on my phone to a server running on my PC. They're in the same network &amp; the server should be compatible with the mobile app, but considering the errors / freeze screens I receive, I don't see what else could be the problem.
Does Java NIO work normally on android at all / could I possibly connect a client on my phone (which is a Galaxy S3, btw -> API 18) to a server on a PC with Windows?
Thank you! :)</p>

<p>//edit: nevermind, the error was 100% dumb. When using an Intent to create a new activity, i created the intent, launched it, and THEN used intentname.putExtra(key, value) - which obviously didn't work. Right now, I got another error because I tried to change a checkBox from a different thread, but the network part worked, so I guess I'll be done soon.</p>