问题描述
我有一个问题,我试图连接到使用MySQL数据库的AsyncTask
。我把连接code在按钮事件,但它给了我的错误,
下面是code:
包com.example.mysqlconnect;
进口java.sql.Connection中;
进口java.lang.DriverManager中;
进口java.sql.ResultSet中;
进口java.sql.ResultSetMetaData;
进口java.sql.Statement中;
进口android.app.Activity;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.TextView;
公共类MainActivity延伸活动{
私有静态最后字符串URL =的jdbc:mysql的://冷/应用程序;
私有静态最后弦乐用户=用户;
私有静态最后字符串传递=密码;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
最后Button按钮=(按钮)findViewById(R.id.button1);
button.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
//执行上的点击动作
连接();
}
});
}
TextView的电视=(TextView中)this.findViewById(R.id.textView1);
私有类连接扩展的AsyncTask<字符串,太虚,字符串> {
@覆盖
保护字符串doInBackground(字符串...网址){
串响应=;
尝试 {
的Class.forName(com.mysql.jdbc.Driver);
连接CON =的DriverManager.getConnection(URL,用户通过);
//的System.out.println(数据库连接成功);
字符串结果=数据库连接成功\ N的;
声明ST = con.createStatement();
结果集RS = st.executeQuery(SELECT * FROM用户);
ResultSetMetaData的RSMD = rs.getMetaData();
而(先将rs.next()){
结果+ = rsmd.getColumnName(1)+:+ rs.getInt(1)+\ N的;
结果+ = rsmd.getColumnName(2)+:+ rs.getString(2)+\ N的;
结果+ = rsmd.getColumnName(3)+:+ rs.getString(3)+\ N的;
}
tv.setText(结果);
}
赶上(例外五){
e.printStackTrace();
tv.setText(e.toString());
}
返回响应;
}
@覆盖
保护无效onPostExecute(字符串结果){
tv.setText(结果);
}
}
公共无效连接(){
接任务=新的连接();
task.execute();
}
}
我是新来的AsyncTask,有人可以帮我找到错误?
这里的logcat的:
10-23 19:08:41.168:W / dalvikvm(1514):主题ID = 1:螺纹退出与未捕获的异常(组= 0xb40ca180)
10-23 19:08:41.178:E / AndroidRuntime(1514):致命异常:主要
10-23 19:08:41.178:E / AndroidRuntime(1514):java.lang.RuntimeException的:无法实例活动ComponentInfo {com.example.mysqlconnect / com.example.mysqlconnect.MainActivity}:显示java.lang.NullPointerException
10-23 19:08:41.178:E / AndroidRuntime(1514):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
10-23 19:08:41.178:E / AndroidRuntime(1514):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-23 19:08:41.178:E / AndroidRuntime(1514):在android.app.ActivityThread.access $ 600(ActivityThread.java:123)
10-23 19:08:41.178:E / AndroidRuntime(1514):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147)
10-23 19:08:41.178:E / AndroidRuntime(1514):在android.os.Handler.dispatchMessage(Handler.java:99)
10-23 19:08:41.178:E / AndroidRuntime(1514):在android.os.Looper.loop(Looper.java:137)
10-23 19:08:41.178:E / AndroidRuntime(1514):在android.app.ActivityThread.main(ActivityThread.java:4424)
10-23 19:08:41.178:E / AndroidRuntime(1514):在java.lang.reflect.Method.invokeNative(本机方法)
10-23 19:08:41.178:E / AndroidRuntime(1514):在java.lang.reflect.Method.invoke(Method.java:511)
10-23 19:08:41.178:E / AndroidRuntime(1514):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
10-23 19:08:41.178:E / AndroidRuntime(1514):在dalvik.system.NativeStart.main(本机方法)
10-23 19:08:41.178:E / AndroidRuntime(1514):由:显示java.lang.NullPointerException
10-23 19:08:41.178:E / AndroidRuntime(1514):在android.app.Activity.findViewById(Activity.java:1794)
10-23 19:08:41.178:E / AndroidRuntime(1514):在com.example.mysqlconnect.MainActivity< INIT>(MainActivity.java:51)
10-23 19:08:41.178:E / AndroidRuntime(1514):在java.lang.Class.newInstanceImpl(本机方法)
10-23 19:08:41.178:E / AndroidRuntime(1514):在java.lang.Class.newInstance(Class.java:1319)
10-23 19:08:41.178:E / AndroidRuntime(1514):在android.app.Instrumentation.newActivity(Instrumentation.java:1023)
10-23 19:08:41.178:E / AndroidRuntime(1514):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
我删除上创建第二个,现在我得到的logcat这个错误:
10-23 19:10:29.129:E / AndroidRuntime(1565):致命异常:主要
10-23 19:10:29.129:E / AndroidRuntime(1565):java.lang.RuntimeException的:无法实例活动ComponentInfo {com.example.mysqlconnect / com.example.mysqlconnect.MainActivity}:显示java.lang.NullPointerException
您的错误是: TextView的电视=(TextView中)this.findViewById(R.id.textView1);
因为它之外声明的onCreate
。
因此,这是你必须做的,申报 TextView的电视;
上方的onCreate
。然后在里面的onCreate
添加电视=(TextView中)findViewById(R.id.textView1);
和消除重复的:
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
I have a problem, I am trying to connect to a MySQL database using AsyncTask
. I put the connection code in the button event but it gives me error,
Here is the code:
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();
}
}
I'm new to AsyncTask, can someone help me finding the errors?
Here's the logcat:
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)
I removed the second on create and now i get this error in 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
Your error is: TextView tv = (TextView)this.findViewById(R.id.textView1);
because it is declared outside of onCreate
.
So this is what you have to do, declare TextView tv;
above onCreate
. Then inside onCreate
you add tv = (TextView) findViewById(R.id.textView1);
And remove the duplicate of:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
这篇关于Java的机器人的AsyncTask + MySQL连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!