gpt4 book ai didi

java - Android 应用程序意外直接崩溃

转载 作者:太空狗 更新时间:2023-10-29 16:34:55 24 4
gpt4 key购买 nike

我正在研究 Android 应用程序的登录和注册功能。用户名和密码保存在ms sql数据库中。起初建立了连接并且应用程序运行良好。但是现在它直接在运行时崩溃,尽管我没有做任何更改。这是我的 Activity 代码和我收到的 Logcat。有人可以帮忙吗?

Activity :

public class Login extends Activity implements OnClickListener{
private static final String TAG = Login.class.getSimpleName();
EditText user;
EditText pass;
Button btnlog;
Button btncan;
String query;
Connection connection;
ArrayList<String> userlist = new ArrayList<String>();
ArrayList<String> passlist = new ArrayList<String>();

private void declarer(){
user = (EditText)findViewById(R.id.editText_name);
pass = (EditText)findViewById(R.id.editText_pass);
btnlog = (Button)findViewById(R.id.login);
//btncan = (Button)findViewById(R.id.cancel);
btnlog.setOnClickListener(this);
btncan.setOnClickListener(this);

query = "SELECT * FROM table ";
connection = CONN("databse","password","db","server");
}
//method to establish a connection with the database
private Connection CONN(String _user, String _pass, String _DB, String _server){

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn = null;
String ConnURL = null;
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
ConnURL = "jdbc:jtds:sqlserver://" + _server + ";" + "databaseName=" + _DB + ";user=" + _user + ";password=" + _pass + ";";
//ConnURL ="jdbc:sqlserver://"+_server+":1433/"+_DB+";";
conn = DriverManager.getConnection(ConnURL);
}
catch (SQLException se){
Log.e("Error", se.getMessage());
}
catch(ClassNotFoundException e){
Log.e("Error",e.getMessage());
}
catch (Exception e){
Log.e("Error",e.getMessage());
}
return conn;
}

//Method to execute sql queries on the database
public void QuerySQL(String CommandDoSQL){
ResultSet rs;
try{
Statement statement = connection.createStatement();
rs = statement.executeQuery(CommandDoSQL);

int i =0;
while(rs.next()){
userlist.add(rs.getString("username"));
passlist.add(rs.getString("password"));
i++;
}
}
catch (Exception e){
Log.e("Error",e.getMessage());
}
}

// Have to implement with the OnClickListner
// onClick is called when a view has been clicked.
@Override
public void onClick(View v){
String un = user.getText().toString();
String pas = pass.getText().toString();

switch (v.getId()){

case R.id.login:

if(isValidUser(un)!= -1 && isValidPassword(pas)!= -1 && isValidUser(un)==isValidPassword(pas)){
Intent i = new Intent(this,Activity2.class);
startActivity(i);
}
else if (isValidPassword(pas) == -1 && isValidUser(un)!= -1) {
pass.setError("Invalid Password");
}
else if (isValidUser(un) == -1 && isValidPassword(pas)!= -1) {
pass.setError("Invalid Username");
}
else if (isValidUser(un)== -1 && isValidPassword(pas)== -1) {
user.setError("Invalid Username");
pass.setError("Invalid Password");
}
break;

case R.id.cancel:
user.setText("");
pass.setText("");
break;

default:
break;
}
}

private int isValidUser(String un) {

for(int i =0;i<userlist.size();i++){
if (un.equals(userlist.get(i))) {
return i;
}
}
return -1;
}
private int isValidPassword(String pass) {

for(int i =0;i<passlist.size();i++){
if (pass.equals(passlist.get(i))) {
return i;
}
}
return -1;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

declarer();
QuerySQL(query);
}
}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Login"
android:id="@+id/Login">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Username"
android:id="@+id/textView4"
android:layout_marginTop="31dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Password"
android:id="@+id/textView5"
android:layout_marginTop="53dp"
android:layout_below="@+id/textView4"
android:layout_toLeftOf="@+id/editText_name"
android:layout_toStartOf="@+id/editText_name" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="Username"
android:ems="10"
android:id="@+id/editText_name"
android:layout_above="@+id/textView5"
android:layout_toRightOf="@+id/textView4"
android:layout_toEndOf="@+id/textView4"
android:layout_marginLeft="27dp"
android:layout_marginStart="27dp" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText_pass"
android:layout_alignBottom="@+id/textView5"
android:layout_alignLeft="@+id/editText_name"
android:layout_alignStart="@+id/editText_name" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/login"
android:layout_marginTop="43dp"
android:layout_below="@+id/editText_pass"
android:layout_alignRight="@+id/editText_pass"
android:layout_alignEnd="@+id/editText_pass" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="@+id/cancel"
android:layout_alignTop="@+id/login"
android:layout_centerHorizontal="true" />

</RelativeLayout>

LOGCAT:

3156-3156/com.developer.user.application D/AndroidRuntime﹕ Shutting down VM
3156-3156/com.developer.user.application E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.developer.user.application, PID: 3156
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.developer.user.application/com.developer.user.application.Login}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.developer.user.application.Login.declarer(Login.java:45)
at com.developer.user.application.Login.onCreate(Login.java:154)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

最佳答案

您正在尝试在没有引用的对象上设置监听器 btncan.setOnClickListener(this);,因为您用设置引用注释了行。尝试取消注释这一行:

    //btncan = (Button)findViewById(R.id.cancel);

private void declarer(){
user = (EditText)findViewById(R.id.editText_name);
pass = (EditText)findViewById(R.id.editText_pass);
btnlog = (Button)findViewById(R.id.login);
//btncan = (Button)findViewById(R.id.cancel);
btnlog.setOnClickListener(this);
btncan.setOnClickListener(this);

query = "SELECT * FROM table ";
connection = CONN("databse","password","db","server");
}

关于java - Android 应用程序意外直接崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31215229/

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