gpt4 book ai didi

java - android.view.View.requestLayout(View.java :16431)

转载 作者:太空宇宙 更新时间:2023-11-04 14:49:39 31 4
gpt4 key购买 nike

我们正在使用 4 版本的 Android 应用程序,我们需要 mysql 连接。我们使用返回数据库结果的 php 文件来执行此操作。以下代码有效,因为如果我们调试项目,我们可以在 System.out.println 中看到正确的数据库结果。线。但是,如果我们尝试更改应用程序中数据库结果的文本,我们的应用程序将无法正确运行,并且错误出现在 t.setText(txt); 中。线。

有人可以帮我解决这个问题吗?您还有其他想法来进行数据库连接还是正确的方法?

非常感谢。

我的代码:

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//FUNCIONA NO BORRAR
final httpHandler handler = new httpHandler();
Thread thread = new Thread()
{

@Override
public void run() {
try {
if(true) {
sleep(1000);
String txt = handler.post("http://www.golftipp.com/pruebas_android/app.php");
TextView t = (TextView) findViewById(R.id.textView2);
t.setText(txt);
System.out.println(txt);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}

};
thread.start();

}

错误

05-30 02:42:50.331    1276-1291/es.softline.connexiodb.app E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-87
Process: es.softline.connexiodb.app, PID: 1276
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:824)
at android.view.View.requestLayout(View.java:16431)
at android.view.View.requestLayout(View.java:16431)
at android.view.View.requestLayout(View.java:16431)
at android.view.View.requestLayout(View.java:16431)
at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:352)
at android.view.View.requestLayout(View.java:16431)
at android.widget.TextView.checkForRelayout(TextView.java:6600)
at android.widget.TextView.setText(TextView.java:3813)
at android.widget.TextView.setText(TextView.java:3671)
at android.widget.TextView.setText(TextView.java:3646)
at es.softline.connexiodb.app.MainActivity$1.run(MainActivity.java:33)

最佳答案

非UI线程无法访问 View ,使用runOnUiThread():

Thread thread = new Thread()
{

@Override
public void run() {
try {
if(true) {
sleep(1000);
String txt = handler.post("http://www.golftipp.com/pruebas_android/app.php");
TextView t = (TextView) findViewById(R.id.textView2);

runOnUiThread(new Runnable() {
@Override
public void run() {
// do UI updates here
t.setText(txt);
}
});

System.out.println(txt);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}

};
thread.start();

关于java - android.view.View.requestLayout(View.java :16431),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23948906/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com