大家好,我是新来的安卓工作室,所以我还在学习。但今天我遇到了一些我无法解决的问题。每次我在asyntax中执行一个sql查询 "select * from databasename"
做一些类似的事情 while(rs.next)
我的应用程序不断崩溃。我确定我从数据库建立了连接。
我甚至尝试了strictmode来查询main,但是同样的结果总是崩溃
我尝试了网上的每一个教程,我复制了每一个东西并下载了它们的源代码,但最终得到了相同的结果。
这是我的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>
这些是我要找的记录
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条答案
按热度按时间uxhixvfz1#
堆栈跟踪中的消息已向您解释了问题:
只有创建视图层次结构的原始线程才能接触其视图
这说明您正在尝试从非ui线程操作视图(例如,更新文本)。杯具:
解决方案:
之后
doInBackground
,onPostExecute
在ui线程上运行,所以移动textShow1.setText(msg);
在那里做doInBackground
返回msg
而不是null
xxe27gdn2#
如果你想在里面做ui操作
doInBackground
,您可以使用runOnUiThread
如下所示