gpt4 book ai didi

java - 将 xml 导入到 Android Studio 文本

转载 作者:行者123 更新时间:2023-12-01 11:07:19 24 4
gpt4 key购买 nike

德斯..

我有下面的代码从 RSS 网页导入 XML,以将 xml 中的文本显示到我的应用程序,在 list 中添加了互联网和网络状态的权限..

import...
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread() {
final TextView urlTextOut = (TextView) findViewById(R.id.result);
final StringBuilder text = new StringBuilder();
@Override
public void run() {
try
{
String str;
URL url = new URL("My url");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
while ((str = in.readLine()) != null) {
text.append(str);
}
in.close();
} catch (IOException ignored)
{
}
urlTextOut.setText(text);
}
}.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

但是当我在 Android 模拟器或手机上直接运行它时出现以下错误:

>    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

.... 你能帮我解决这个问题

最佳答案

嗨,正如我告诉您的,您创建了新线程,并在其中更新了 TextView 。这就是你更新的 View 不是同一个线程的根本原因。理想情况下,在 android 中只有 UI 线程可以触摸 View 进行更新。让我们来吹一下你的代码。

import...
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView urlTextOut = (TextView) findViewById(R.id.result);
new Thread() {

final StringBuilder text = new StringBuilder();
@Override
public void run() {
try
{
String str;
URL url = new URL("My url");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
while ((str = in.readLine()) != null) {
text.append(str);
}
in.close();
} catch (IOException ignored)
{
}
runOnUiThread(new Runnable() {
@Override
public void run() {
urlTextOut.setText(text);
}
});

}
}.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

关于java - 将 xml 导入到 Android Studio 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32805524/

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