从mysql检索数据时应用崩溃

时间:2018-10-08 10:41:35

标签: android mysql crash

大家好,我是android studio的新手,因此我仍然在学习。但是今天我遇到了我无法解决的问题。每当我在Asyntax中执行诸如"select * from databasename"之类的SQL查询并执行诸如while(rs.next)之类的事情时,我的应用就会不断崩溃。我确定我是从数据库建立连接的。

ps。我什至尝试使用strictmode在主目录中进行查询,但相同的结果不断崩溃

我已经尝试了所有在线教程,直到我复制了每件事并下载了源代码,但最终还是得到了相同的结果。

这是我的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);
    }
}
}

这是我的xml文件,我认为这没有任何问题。

<?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> 

这就是我正在获得的logcat

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) 

2 个答案:

答案 0 :(得分:0)

堆栈跟踪中的消息已向您解释了该问题:

  

只有创建视图层次结构的原始线程可以触摸其视图

这告诉我您正在尝试从非UI线程操纵视图(例如,更新文本)。铜杯:

textShow1.setText(msg);

解决方案:

doInBackground之后,onPostExecute在UI线程上运行,因此将textShow1.setText(msg);移动到那里,并使doInBackground返回msg而不是null < / p>

答案 1 :(得分:-1)

如果您想在doInBackground内部进行UI操作,则可以按以下方式使用runOnUiThread

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

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

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