gpt4 book ai didi

Java Android AsyncTask + mysql连接

转载 作者:行者123 更新时间:2023-11-29 05:33:06 25 4
gpt4 key购买 nike

我有一个问题,我正在尝试使用 AsyncTask 连接到 MySQL 数据库。我将连接代码放在按钮事件中,但它给了我错误,

代码如下:

    package com.example.mysqlconnect;


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





import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

private static final String url = "jdbc:mysql://cool/app";
private static final String user = "user";
private static final String pass = "password";

/** Called when the activity is first created. */

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


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Connect();
}
});
}

TextView tv = (TextView)this.findViewById(R.id.textView1);
private class Connect extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";

try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, pass);
// System.out.println("Database connection success");

String result = "Database connection success\n";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from users");
ResultSetMetaData rsmd = rs.getMetaData();

while(rs.next()) {
result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
result += rsmd.getColumnName(3) + ": " + rs.getString(3) + "\n";
}
tv.setText(result);
}
catch(Exception e) {
e.printStackTrace();
tv.setText(e.toString());
}
return response;


}

@Override
protected void onPostExecute(String result) {
tv.setText(result);
}
}

public void Connect() {
Connect task = new Connect();
task.execute();

}
}

我是 AsyncTask 的新手,有人可以帮我找出错误吗?

这是日志:

    10-23 19:08:41.168: W/dalvikvm(1514): threadid=1: thread exiting with uncaught exception (group=0xb40ca180)
10-23 19:08:41.178: E/AndroidRuntime(1514): FATAL EXCEPTION: main
10-23 19:08:41.178: E/AndroidRuntime(1514): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mysqlconnect/com.example.mysqlconnect.MainActivity}: java.lang.NullPointerException
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.os.Looper.loop(Looper.java:137)
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-23 19:08:41.178: E/AndroidRuntime(1514): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 19:08:41.178: E/AndroidRuntime(1514): at java.lang.reflect.Method.invoke(Method.java:511)
10-23 19:08:41.178: E/AndroidRuntime(1514): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-23 19:08:41.178: E/AndroidRuntime(1514): at dalvik.system.NativeStart.main(Native Method)
10-23 19:08:41.178: E/AndroidRuntime(1514): Caused by: java.lang.NullPointerException
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.app.Activity.findViewById(Activity.java:1794)
10-23 19:08:41.178: E/AndroidRuntime(1514): at com.example.mysqlconnect.MainActivity.<init>(MainActivity.java:51)
10-23 19:08:41.178: E/AndroidRuntime(1514): at java.lang.Class.newInstanceImpl(Native Method)
10-23 19:08:41.178: E/AndroidRuntime(1514): at java.lang.Class.newInstance(Class.java:1319)
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)

我在创建时删除了第二个,现在我在 logcat 中收到此错误:

10-23 19:10:29.129: E/AndroidRuntime(1565): FATAL EXCEPTION: main
10-23 19:10:29.129: E/AndroidRuntime(1565): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mysqlconnect/com.example.mysqlconnect.MainActivity}: java.lang.NullPointerException

最佳答案

您的错误是:TextView tv = (TextView)this.findViewById(R.id.textView1); 因为它是在 onCreate 之外声明的。

所以这就是您必须要做的,在 onCreate 上方声明 TextView tv;。然后在 onCreate 中添加 tv = (TextView) findViewById(R.id.textView1);

并删除重复的:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

关于Java Android AsyncTask + mysql连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13037730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com