Vladimir Chudnofsky Vladimir Chudnofsky - 5 months ago 13
Android Question

read text file and adding it line by line to multiple textviews not at once

I wrote a program that read text file check if the first element is 1 and display it to the textview(TextViewDisk1) and check if its 2 and so on.It did work well but only for the first line. After the program read the next line the app stops.


logcat


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

How do I solve this problem?


Code


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

Answer

if you want to modify textview from a thread that isn't the ui thread you need to use runOnuiThread(..). Here is an example.`

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

if your file isn't of 2400 lines it will throw a nullPointerException.

Comments