1

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

这是代码:

    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
4

1 回答 1

3

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

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

并删除以下副本:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
于 2012-10-23T19:33:33.993 回答