当前位置: 首页 > 知识库问答 >
问题:

Android错误:只有创建视图层次结构的原始线程才能触摸其视图

洪飞白
2023-03-14

我读了其他帖子,但没有解决我的问题。我是Android的新手。我必须连接到MSSQL,但我得到了原始线程错误。我如何解决这个问题?谢谢

这是我的代码:

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();
    }

}

在KuyrugaEkle函数中,我动态更改布局,并且我在该函数中收到错误。

谢谢

共有2个答案

桓风史
2023-03-14

创建MyThread的对象并在runOnUiThread方法中使用它

MyThread mt = new MyThread();

获取活动的上下文并使用以下代码

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

将Application上下文传递给MyThread类的构造函数并如上所述使用它。

束新
2023-03-14

您不允许从main以外的线程修改UI,您可以在函数kuyrukKontrol中进行修改。使用处理程序或runOnUiThread在主线程上执行此操作。

 类似资料: