我编写了一个读取文本文件的程序,检查第一个元素是否为1并将其显示到textview(TextViewDisk1)并检查它是否为2等等。它确实运行良好但仅适用于第一行。程序读完下一行后,应用停止。
logcat的
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views
。
如何解决这个问题?
代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Read text file and show number on the textview
new Thread(new Runnable() {
@Override
public void run() {
final TextView TextDisk1 = (TextView) findViewById(R.id.TextDisk1);
final TextView TextDisk2 = (TextView) findViewById(R.id.TextDisk2);
final TextView TextDisk3 = (TextView) findViewById(R.id.TextDisk3);
final TextView TextDisk4 = (TextView) findViewById(R.id.TextDisk4);
final TextView TextDisk5 = (TextView) findViewById(R.id.TextDisk5);
try {
InputStream stream = getAssets().open("Messwerte.txt");
InputStreamReader reader = new InputStreamReader(stream);
BufferedReader buffer = new BufferedReader(reader);
String lines;
for (int i = 0; i < 2400; i++) {
lines = buffer.readLine();
String[] line = lines.split("\t");
String first = line[0];
String second = line[1];
String third = line[2];
TextDisk3.setText(third);
if(first.equals("1"))
{
TextDisk1.setText(second);
}
else if(first.equals("2"))
{
TextDisk2.setText(second);
}
else if(first.equals("4"))
{
TextDisk4.setText(second);
}
else if(first.equals("5"))
{
TextDisk5.setText(second);
}
i++;
Thread.sleep(6000);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
答案 0 :(得分:0)
如果你想从一个不需要使用runOnuiThread(..)的ui线程的线程修改textview。 这是一个例子。
new Thread(new Runnable() {
@Override
public void run() {
final TextView TextDisk1 = (TextView) findViewById(R.id.TextDisk1);
final TextView TextDisk2 = (TextView) findViewById(R.id.TextDisk2);
final TextView TextDisk3 = (TextView) findViewById(R.id.TextDisk3);
final TextView TextDisk4 = (TextView) findViewById(R.id.TextDisk4);
final TextView TextDisk5 = (TextView) findViewById(R.id.TextDisk5);
try {
InputStream stream = getAssets().open("Messwerte.txt");
InputStreamReader reader = new InputStreamReader(stream);
BufferedReader buffer = new BufferedReader(reader);
String lines;
for (int i = 0; i < 2400; i++) {
lines = buffer.readLine();
String[] line = lines.split("\t");
String first = line[0];
String second = line[1];
String third = line[2];
WriteText(TextDisk3, third);
if (first.equals("1")) {
WriteText(TextDisk1, second);
}
else if (first.equals("2")) {
WriteText(TextDisk2, second);
} else if (first.equals("4")) {
WriteText(TextDisk4, second);
}
else if (first.equals("5")) {
WriteText(TextDisk5, second);
}
i++;
Thread.sleep(6000);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
public void WriteText(final TextView textview, final String value) {
runOnUiThread(new Runnable() {
@Override
public void run() {
textview.setText(value);
}
});
}
}
如果您的文件不是2400行,则会抛出nullPointerException。