Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9096637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:54:09+00:00 2026-06-16T23:54:09+00:00

I get this exception and I don’t know why, that’s the classes I have:

  • 0

I get this exception and I don’t know why, that’s the classes I have:
An activity which starts a service which starts a class which is a ScheduledExecutorService that connect to the db(that’s why the connection thing).

When I tried to connect through my service I understood why I got this exception but now I created a new class/task to do that but I’m still getting this exception.

I didn’t attached code since it’s alot of code and the problem might be in the way I managed this system and not in the code itself, if you would like to see anything else let me know.


See Managing a connection to the mysql through a service for more information on why a Thread isn’t appropriate in this case.

EDIT
code attachment:
this is my third class that sends details to the db.

public class SendLocation 
{
    private String lastAddress;
    private int id;
    private Connection conn;
    private PreparedStatement stmt;
    private LocationService ls1;
    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

    public SendLocation(int id,LocationService ls1)
    {
        this.lastAddress = "";
        this.id = id;
        this.conn = null;
        this.sqlConnect();
        this.sqlInsertStatement();
        this.ls1 = ls1;
    }
    public void send()
    {
        final Runnable sender = new Runnable() 
        {
            public void run() 
            { 
                if(!lastAddress.equals(ls1.getAddress()) && !ls1.getAddress().equals(""))
                {
                    lastAddress = ls1.getAddress();
                    try 
                    {
                        stmt.setInt(1, id);
                        stmt.setString(2, lastAddress);
                        stmt.execute();
                    } 
                    catch (SQLException e) {
                        e.printStackTrace();
                    } 
                }
            }
        };


        final ScheduledFuture sendHandle = 
                scheduler.scheduleAtFixedRate(sender, 1, 1, TimeUnit.SECONDS);

        scheduler.schedule(new Runnable()
        {
            public void run() 
            { 
                sendHandle.cancel(true); 
            }
        }, 60 * 60, TimeUnit.SECONDS);
    }



     public boolean sqlConnect()
        {
            try {
                Class.forName(Config.DRIVER).newInstance();
                DriverManager.setLoginTimeout(100);
                this.conn = DriverManager.getConnection(Config.URL+Config.DBNAME,
                        Config.USERNAME,Config.PASSWORD);
                return true;

            }catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
     public void sqlInsertStatement()
     {
         try {
             this.stmt = conn.prepareStatement
                     ("INSERT INTO locations(id, location) VALUES (?,?)");
         }catch (SQLException e1) {
             e1.printStackTrace();
         }
     }
}

EDIT

I changed the task into a thread here is the new code:

public class SendLocation extends Thread
{
    private String lastAddress;
    private int id;
    private Connection conn;
    private PreparedStatement stmt;
    private LocationService ls1;
    private boolean run;

    public SendLocation(int id,LocationService ls1)
    {
        this.lastAddress = "";
        this.id = id;
        this.conn = null;
        this.sqlConnect();
        this.sqlInsertStatement();
        this.ls1 = ls1;
        this.run = true;
    }
    public void run() 
    { 
        while(run)
        {
            if(!lastAddress.equals(ls1.getAddress()) && !ls1.getAddress().equals(""))
            {
                lastAddress = ls1.getAddress();
                try 
                {
                    stmt.setInt(1, id);
                    stmt.setString(2, lastAddress);
                    stmt.execute();
                    sleep(1000);
                } 
                catch (Exception e) {
                    e.printStackTrace();
                } 
            }
        }
    }       
    public void destroy()
    {
        this.run = false;
    }
    public boolean sqlConnect()
    {
        try {
            Class.forName(Config.DRIVER).newInstance();
            DriverManager.setLoginTimeout(100);
            this.conn = DriverManager.getConnection(Config.URL+Config.DBNAME,
                    Config.USERNAME,Config.PASSWORD);
            return true;

        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
     public void sqlInsertStatement()
     {
         try {
             this.stmt = conn.prepareStatement
                     ("INSERT INTO locations(id, location) VALUES (?,?)");
         }catch (SQLException e1) {
             e1.printStackTrace();
         }
     }
}

How come now that I’m getting the NetworkOnMainThread exception??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:54:11+00:00Added an answer on June 16, 2026 at 11:54 pm

    The exception means that you are performing a long term operation on the UI thread, like a network operation eg. Since android 3, those operations are not allowed and the exception is thrown. So, in order to avoid the exception you should execute your code in a async way. You do not have to create class, but you have to create a class that extend Thread or AsyncTask.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

why do I get this exception?please suggest any solutions.i have properly copied mail.jar and
I know i will get this exception when i try to modify or remove
I don't know why this is happening, but I get this error: -[__NSArrayM section]:
I don't understand why i am getting this exception. This is the code that
I have problem and i don't know how to solve this simple, i have
This is my code below. The program runs fine. I don't get any exceptions.
I get this exception on my Windows 7 64bit in application running in VS
Several random customers get this exception every time I update my Android app. I've
why do i get this exception? Map myHash = null myHash = (HashMap)Collections.synchronizedMap(new HashMap());
I'm trying the basic jMonkey but I get this exception jun 27, 2012 9:25:08

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.