简体   繁体   English

线程未在android中运行

[英]Thread is not running in android

I'm trying to connect my android application with mysql database using jdbc driver. 我正在尝试使用jdbc驱动程序将android应用程序与mysql数据库连接。 I searched a lot and found that using thread or AsyncTask we can connect to the mysql directly. 我进行了很多搜索,发现使用线程或AsyncTask可以直接连接到mysql。 But i found difficult in asynctask, So i'm using thread to do that. 但是我发现在asynctask中很难,所以我正在使用线程来做到这一点。 Here is my code : 这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_customer);
    TextView t = (TextView) findViewById(R.id.Search_Customer_textView1);
    Button b = (Button) findViewById(R.id.Search_Customer_button1);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Thread timer=new Thread(){
                public void run(){
                    try {
                        Class.forName("com.mysql.jdbc.Driver");
                        con = DriverManager.getConnection("jdbc:mysql://192.168.1.102:3306/outsidelaundry", "root", "");
                        Statement stmt = con.createStatement();
                        String query = "select Name from nonmember";
                        ResultSet rs = stmt.executeQuery(query);        
                        while(rs.next())
                        {
                            val = val + rs.getString(1) + "\n";
                        }
                        Toast.makeText(getApplicationContext(), val, Toast.LENGTH_LONG).show();
                        con.close();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            timer.start();
            Toast.makeText(getApplicationContext(), "pressed", Toast.LENGTH_LONG).show();
        }
    });

    }

When i run the program thread is not starting/running. 当我运行时,程序线程未启动/正在运行。 I didn't get any exception. 我没有例外。 Please help me with this. 请帮我解决一下这个。

You are displaying a Toast inside a thread. 您正在线程中显示Toast。 You should update ui on ui thread. 您应该在ui线程上更新ui。

Inside thread's run method use runOnUiThread . 内部线程的run方法使用runOnUiThread runOnUiThread is a method of your activity class. runOnUiThread是您的活动类的方法。 Add @Override 添加@Override

    Thread timer=new Thread(){
        @Override
        public void run(){
          Log.i("Thread","Running"); 
          // you should see the log message if the thread runs
            try {

                    // do othere operations
                runOnUiThread(new Runnable() {
                        @Override 
                    public void run() {
                        // dispaly toast here;
                    }
                });

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    timer.start();

You can use asynctask also. 您也可以使用asynctask。

You need to call Toast on runOnUiThread() method 您需要在runOnUiThread()方法上调用Toast

activity.runOnUiThread(new Runnable() {
    public void run() {
        Toast.makeText(getApplicationContext(), val, Toast.LENGTH_LONG).show();
    }
});

By Looking at your code, I can see that exception is getting generated but you have cached it so not as visible force close is happening while running the code. 通过查看您的代码,我可以看到正在生成异常,但是您已经缓存了该异常,因此不会在运行代码时发生可见的强制关闭。

Simple take on textbox in the layout and try to set the text for that as response. 在布局中对文本框进行简单处理,然后尝试将其文本设置为响应。 which confirms that you have no problem with JDBC or database. 这确认您对JDBC或数据库没有问题。

and if that succeed then AsynTask is the best one to use here. 如果成功了,那么AsynTask就是在这里使用的最好的工具。

Try this... 尝试这个...

  try{
     timer.join();
      }  
   catch (Exception e) {
                    e.printStackTrace();
                }

We have to wait until the thread finish it work. 我们必须等到线程完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM