GK Soft
GK Soft

Reputation: 103

Android Error : Only the original thread that created a view hierarchy can touch its views

I read other posts but didnt solved my problem. I am newbie on Android. I have to connect to MSSQL but i am getting the original thread error. How can i solve this? Thanks.

Here is my code :

private class MyThread implements Runnable {
    public void run() {

        durumKontrol();
        if (isConnected) {

            new Thread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub

                    kuyrukKontrol();
                }

            }).start();

        } else {
            Toast toast = Toast
                    .makeText(
                            getApplicationContext(),
                            "Ağa erişilemiyor. Lütfen kablosuz ağın açık olduğundan emin olunuz",
                            Toast.LENGTH_LONG);
            toast.show();
        }

        tick_Handler.postDelayed(tick_thread, firstDelay);
        firstDelay = 10000;
    }

}private void kuyrukKontrol() {
    // TODO Auto-generated method stub

    // Hücrenin sipariş ettiği ve henüz getirilmemiş malzemelerin listesi
    // çekiliyor
    TextView tvIP = (TextView) findViewById(R.id.tvIP);
    tvIP.setText(HatAdi + "-" + HucreNo);


    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        Connection conn = DriverManager
                .getConnection("jdbc:jtds:sqlserver://x.x.x.x:1433",
                        "xx", "xxxxx");

        Statement query = conn.createStatement();

        // İlk olarak bu forklifte ait teslim edilmeyen bir talep var mı
        ResultSet rs = query
                .executeQuery("select * from MOBILE.dbo.fos_SS where hucreNo = '" + HucreNo + "' and sonDurum != 'T'");
        while (rs.next()) {

            KuyrugaEkle(rs.getString("malzemeNo"),rs.getString("sonDurum"),rs.getString("sonIslemZamani"));

        }

        rs.close();
        conn.close();

    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

and in KuyrugaEkle function i dynamically change the layout, and i am getting error in that function.

Thanks.

Upvotes: 1

Views: 1804

Answers (2)

Arun Paarthi
Arun Paarthi

Reputation: 643

Create a Object of the MyThread and use it in runOnUiThread method

MyThread mt = new MyThread();

Get the Activity's context and use the following code

Activity a=(Activity)context;
            a.runOnUiThread(mt);

Pass the Applications context to the MyThread class's constructor and use it as I mentioned above.

Upvotes: 1

MByD
MByD

Reputation: 137282

You are not allowed to modify the UI from thread other than main, and you do that in the function kuyrukKontrol. Use Handler or runOnUiThread to perform it on the main thread.

Upvotes: 1

Related Questions