0

I need to connect to postgreSQL. When I try to run this code I got exception caused by: NetworkOnMyThreadExcepion. I've made new Thread, but it's still same problem.

...
final ErrorFragment error = new ErrorFragment();
...

new Thread(new Runnable(){
    public void run(){
        /* Properties prop, String url*/
        try {
            Connection conn = DriverManager.getConnection(url, prop);
        } catch (final SQLException e) {
            MainActivity.this.runOnUiThread(new Runnable(){
                public void run(){
                    error.setArguments(exceptionToBundle(e));
                    error.show(getFragmentManager(), "tagError");
                }
            });
        }
    }
}).run();

...
private Bundle exceptionToBundle(Exception e){
...
}
michal.jakubeczy
  • 4,457
  • 1
  • 30
  • 43

1 Answers1

0

As @tyczj already mentioned, you have to use start() to start the Thread. run() is the method which will be executed in the new Thread. If you call run() directly you run your code on the current Thread.

So instead of this:

new Thread(...).run();

try this:

new Thread(...).start();
Xaver Kapeller
  • 46,604
  • 11
  • 87
  • 82