gpt4 book ai didi

android - 方法调用崩溃 "Could not execute method of the activity"

转载 作者:行者123 更新时间:2023-12-02 08:19:07 24 4
gpt4 key购买 nike

我正在尝试实现从我的 Android 应用程序到存储在 MS SQL Server 2008 中的数据库的登录系统。与服务器的连接正常,当我调用 logIn 方法让用户在连接帮助程序类上进行连接时出现问题比较密码匹配。

这是单击登录按钮时执行的函数:

public void onLogInButtonClicked(View v){
final EditText login = (EditText) findViewById(R.id.login);
final EditText password = (EditText) findViewById(R.id.password);

String userName = login.getText().toString();
String pass = password.getText().toString();
System.out.println("Pass Entered:" + pass);
System.out.println("User Entered" + userName);


if (connector.logIn(userName, pass)){

//Creates Intent from new Activity to be launched
Intent k = new Intent(this, MainActivity.class);

//Sends login name to activity k
k.putExtra("loginName", userName);
//Starts new Activity
startActivity(k);


}
else
{
//toast log in unsuccessful
}
}

Connector类的logIn方法如下:

boolean logIn(String name, String password){
System.out.println("I'm in LogIn method");
java.sql.ResultSet result = null;
boolean log = false;
try {
connection = this.getConnection();
if (connection != null) {

//String for the login query
String statement = "SELECT Password FROM Users WHERE UserName = '"
+ name + "'";
System.out.println(statement);

Statement select = connection.createStatement();
result = select.executeQuery(statement);
System.out.println(result.getString(1));
if(result.getString(1) == password) log = true;

result.close();
result = null;
closeConnection();
}
else {
System.out.println("Error: No active Connection");
return log;
}
} catch (Exception e) {
e.printStackTrace();
}
return log;
}

我什至没有在控制台中看到“我正在登录方法”,这让我猜测应用程序在进入它之前就崩溃了,尽管崩溃发生在调用该方法的行中。

这是日志:

04-25 23:32:45.855: E/AndroidRuntime(795): FATAL EXCEPTION: main
04-25 23:32:45.855: E/AndroidRuntime(795): java.lang.IllegalStateException: Could not execute method of the activity
04-25 23:32:45.855: E/AndroidRuntime(795): at android.view.View$1.onClick(View.java:2144)
04-25 23:32:45.855: E/AndroidRuntime(795): at android.view.View.performClick(View.java:2485)
04-25 23:32:45.855: E/AndroidRuntime(795): at android.view.View$PerformClick.run(View.java:9080)
04-25 23:32:45.855: E/AndroidRuntime(795): at android.os.Handler.handleCallback(Handler.java:587)
04-25 23:32:45.855: E/AndroidRuntime(795): at android.os.Handler.dispatchMessage(Handler.java:92)
04-25 23:32:45.855: E/AndroidRuntime(795): at android.os.Looper.loop(Looper.java:123)
04-25 23:32:45.855: E/AndroidRuntime(795): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-25 23:32:45.855: E/AndroidRuntime(795): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 23:32:45.855: E/AndroidRuntime(795): at java.lang.reflect.Method.invoke(Method.java:507)
04-25 23:32:45.855: E/AndroidRuntime(795): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-25 23:32:45.855: E/AndroidRuntime(795): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-25 23:32:45.855: E/AndroidRuntime(795): at dalvik.system.NativeStart.main(Native Method)
04-25 23:32:45.855: E/AndroidRuntime(795): Caused by: java.lang.reflect.InvocationTargetException
04-25 23:32:45.855: E/AndroidRuntime(795): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 23:32:45.855: E/AndroidRuntime(795): at java.lang.reflect.Method.invoke(Method.java:507)
04-25 23:32:45.855: E/AndroidRuntime(795): at android.view.View$1.onClick(View.java:2139)
04-25 23:32:45.855: E/AndroidRuntime(795): ... 11 more
04-25 23:32:45.855: E/AndroidRuntime(795): Caused by: java.lang.NullPointerException

提前致谢!

最佳答案

听起来您的连接器对象为空。您是否忘记初始化它或范围不正确?如果这不足以帮助您找到连接器的初始化位置,请发布它

关于android - 方法调用崩溃 "Could not execute method of the activity",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10325364/

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