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

Viewed 23
package com.example.acrs;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class RandomShit extends AppCompatActivity {

    Button button;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_random_shit);
        button = (Button) findViewById(R.id.vikrambgm);
        textView = (TextView) findViewById(R.id.textViewPunde);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RandomTask randomTask = new RandomTask();
                randomTask.execute();
            }
        });
    }

    class RandomTask extends AsyncTask<Void, Void, Void> {
        String records = "", error = "";

        ProgressDialog pd;

        @Override
        protected void onPreExecute() { //when person clicks button, will show loading screen before import happens
            super.onPreExecute();
            pd = new ProgressDialog(RandomShit.this);
            pd.setMessage("loading");
            pd.show();
        }

        @Override
        protected Void doInBackground(Void... voids) { //take values from excel and put in sql

            try {
                Class.forName("com.mysql.jdbc.Driver");
                //Localhost Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.18.7:3325/acrs", "fypAcrs", "acrs");
                Connection connection = DriverManager.getConnection("jdbc:mysql://0104-acrs.mysql.database.azure.com:3306/azuredb", "VSVP", "VVSP0104!");
                Statement statement = connection.createStatement();
                ResultSet rs = statement.executeQuery("SELECT * FROM owner_data");
                while (rs.next()) {
                    String name = rs.getString("owner_name");
                    textView.setText(name);
                }


            } catch (Exception e) {
                error = e.toString();
                Log.i("loosu", e.toString());
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            textView.setText(records);

            if (error != "")
                textView.setText(error);
            super.onPostExecute(aVoid);
            if (pd != null)
            {
                pd.dismiss();
            }

            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(RandomShit.this, error, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}

Keep getting the error whenever I press the button. This does not make sense to me. Please ignore the class name as I wrote that out of frustration of this not working. android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. This is the error which keeps appearing.

0 Answers
Related