简体   繁体   中英

App crashing while retrieving data from mysql

Hi guys im new to android studio so im still learning. But today I encountered something which I cant resolve. Everytime I execute inside Asyntax an sql query like "select * from databasename" and do something like while(rs.next) my app keeps on crashing. I made sure I made the connection from the database.

ps. i even tried the strictmode to query in the main but same result keeps crashing

I have tried every tutorial online to the point I copied every single thing and downloaded their source code but I ended up with the same result.

This is my MainActivity.java

public class MainActivity extends AppCompatActivity {

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

    Button btnShow = findViewById(R.id.btnShow);
    btnShow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            GetData retrieveData = new GetData();
            retrieveData.execute();
        }
    });
}
 private class GetData extends AsyncTask<String,String,String>{
    String msg="";
    final TextView textShow = findViewById(R.id.txtView1);
    final TextView textShow1 = findViewById(R.id.txtView);

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://" + ConnectURL.DATABASE_URL + "/" + ConnectURL.DATABASE_NAME;

    @Override
    protected void onPreExecute(){
        String Xmsg="Connecting to database...";
        textShow.setText(Xmsg);
    }
    @Override
    protected String doInBackground(String... params) {
        Connection conn=null;
        Statement stmt=null;

        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, ConnectURL.USERNAME, ConnectURL.PASSWORD);
            stmt = conn.createStatement();
            String sql = "Select * from poultryinfo";
            ResultSet rs = stmt.executeQuery(sql);
            ResultSetMetaData rmrs = rs.getMetaData();

            msg="Connection completed";

           while(rs.next()) {
               msg += rmrs.getColumnName(2) + ":" + rs.getString("valName") + "\n";
            }

            textShow1.setText(msg);

            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException connError){
            msg="An exception was thrown JDBC"+ connError.toString();
            connError.printStackTrace();
        } catch (ClassNotFoundException e) {
            msg = "A class not found exception" + e.toString();
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
                msg = e.toString();
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
                msg = e.toString();
            }
        }
        return null;
    }
    @Override
    protected void  onPostExecute(String msg){
        textShow.setText(this.msg);
    }
}
}

And this is my xml file I dont think theres anything wrong with this.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
    android:id="@+id/txtView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="154dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="154dp"
    android:text="Hello World!"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/btnShow" />

<TextView
    android:id="@+id/txtView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="154dp"
    android:layout_marginTop="12dp"
    android:layout_marginEnd="154dp"
    android:text="Hello World!"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/txtView1" />

<Button
    android:id="@+id/btnShow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="148dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="148dp"
    android:text="@string/show"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout> 

and these are the logcat im getting

10-08 18:33:58.620 25502-25502/? E/Zygote: v2
10-08 18:33:58.620 25502-25502/? E/Zygote: accessInfo : 0
10-08 18:34:00.250 25502-25502/com.example.jade.connectionpractice E/Qmage: 
isQIO : stream is not a QIO file
10-08 18:34:02.830 25502-25813/com.example.jade.connectionpractice 
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.jade.connectionpractice, PID: 25502
java.lang.RuntimeException: An error occurred while executing 
 doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:309)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    at java.lang.Thread.run(Thread.java:818)
 Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:8355)
    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1327)
    at android.view.View.requestLayout(View.java:20170)
    at android.view.View.requestLayout(View.java:20170)
    at android.view.View.requestLayout(View.java:20170)
    at android.view.View.requestLayout(View.java:20170)
    at android.view.View.requestLayout(View.java:20170)
    at android.view.View.requestLayout(View.java:20170)
    at android.support.constraint.ConstraintLayout.requestLayout(ConstraintLayout.java:3172)
    at android.view.View.requestLayout(View.java:20170)
    at android.widget.TextView.checkForRelayout(TextView.java:8425)
    at android.widget.TextView.setText(TextView.java:5011)
    at android.widget.TextView.setText(TextView.java:4836)
    at android.widget.TextView.setText(TextView.java:4811)
    at com.example.jade.connectionpractice.MainActivity$GetData.doInBackground(MainActivity.java:65)
    at com.example.jade.connectionpractice.MainActivity$GetData.doInBackground(MainActivity.java:33)
    at android.os.AsyncTask$2.call(AsyncTask.java:295)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
    at java.lang.Thread.run(Thread.java:818) 

The message in the stack trace has explained the issue to you:

Only the original thread that created a view hierarchy can touch its views

This tells me you are trying to manipulate a view (eg update text) from non UI thread. The cuplrit:

textShow1.setText(msg);

Solution:

after doInBackground , onPostExecute runs on the UI thread, so move textShow1.setText(msg); there and make doInBackground return msg instead of null

If you want to do UI oprerations inside doInBackground , you can use runOnUiThread as below

@Override
protected String doInBackground(String... params) {

    runOnUiThread(new Runnable() {
        public void run() {

            //do your UI operations here
            textShow1.setText(msg);
         }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM