Skip to content

Instantly share code, notes, and snippets.

@commonsguy
Created May 31, 2015 13:26
Show Gist options
  • Save commonsguy/729d6355091158840c40 to your computer and use it in GitHub Desktop.
Save commonsguy/729d6355091158840c40 to your computer and use it in GitHub Desktop.
Attempt to Reproduce Error From http://stackoverflow.com/q/30557528/115145
package com.commonsware.myapplication;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExecutorService executorService = Executors.newFixedThreadPool(1);
executorService.submit(new Runnable() {
@Override
public void run() {
new OutgoingSync(MainActivity.this);
}
});
}
static class OutgoingSync {
public OutgoingSync(Context context){
Log.e("OutgoingSync thread", Thread.currentThread().getName()); // Output "pool-2,thread-1"
doSomeStuff();
}
private void doSomeStuff() {
new UploadPhotosToServer();
}
}
static class UploadPhotosToServer {
public UploadPhotosToServer() {
Log.e("Upload photos thread", Thread.currentThread().getName()); // Output is "main"
// And the following network-related code throws a NetworkOnMainThreadException (because it is run on the main thread)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment