好日子编码员。我对android studio很新。这是我第一次将Android应用程序连接到SQL服务器。我一直使用SQLite,这从来都不是问题。
目前我正在尝试使用带模拟器的jtds-1.3.1连接到SQL数据库。我的应用程序只是在没有使用断点的情况下崩溃。
我的logcat向我提供了这个:
---------崩溃的开始
08-22 06:00:31.278 3273-3273/com.example.drizzybass.skyetek_login_with_sqlserver E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.drizzybass.skyetek_login_with_sqlserver, PID: 3273
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.drizzybass.skyetek_login_with_sqlserver/com.example.drizzybass.skyetek_login_with_sqlserver.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app
老实说,我不知道从哪里开始调试我的应用程序而不通过断点。我将从Mainactivity到onCreate方法的断点设置为不占优势。
我的MainActivity代码如下所示:
EditText user = (EditText) findViewById(R.id.txtUserName);
EditText password = (EditText) findViewById(R.id.txtPassword);
Button btnLogin = (Button) findViewById(R.id.btnLogin);
String strUser = user.getText().toString();
String strPass = password.getText().toString();
String username, pass, db, ip;
Connection con;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Declaring server IP, username, database name and password
ip = "xxx"; //xxx isnt the actually information
db = "xxx";
username = "xxx";
pass = "xxx";
btnLogin.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
CheckLogin checkLogin = new CheckLogin();
checkLogin.execute("");
}
});
}
public class CheckLogin extends AsyncTask<String, String, String>
{
String x = "";
Boolean isSuccess = false;//use to check whether the login fails or not
protected String doInBackground(String... params)
{
if(strUser.trim().equals("") || strPass.trim().equals(""))
{
x = "Please enter Username and Password";
}
else
{
try
{
con = connectionclass(username, pass, db, ip);
if(con == null)
{
x = "Please check your internet connection";
}
else
{
String query = "select * from tblLogin where username = " + strUser.toString() + " and pass_word = " + strPass.toString();
Statement stat = con.createStatement();
ResultSet rs = stat.executeQuery(query);
if(rs.next())
{
x = "Login Successful";
isSuccess = true;
con.close();
}
else
{
x = "Invalid Credentials";
isSuccess = false;
}
}
}
catch (Exception ex)
{
isSuccess = false;
x = ex.getMessage();
}
}
return x;
}
public Connection connectionclass(String user, String password, String database, String server) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection connection = null;
String ConnectionURL = null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
ConnectionURL = "jdbc:jtds:sqlserver://" + server + database + ";user=" + user + ";password=" + password + ";";
connection = DriverManager.getConnection(ConnectionURL);
}
catch (SQLException ae)
{
Log.e("error here 1 : ", ae.getMessage());
}
catch (ClassNotFoundException e)
{
Log.e("error here 2 : ", e.getMessage());
}
catch (Exception ex)
{
Log.e("error here 3 : ", ex.getMessage());
}
return connection;
}
}
我的构建gradle模块应用程序如下所示:
dependencies
{
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.3.1'
compile project(':jtds-1.3.1')
}
最后我的设计包含:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.drizzybass.skyetek_login_with_sqlserver.MainActivity">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="40sp">
<ImageView
android:src="@drawable/skyelogo"
android:textAlignment="center"
android:layout_weight="1"
android:layout_height="92dp"
android:layout_gravity="center_horizontal"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/txtUserName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="40sp"
android:layout_weight="1"
android:gravity="center"
android:hint="Username"
android:inputType="textPersonName"
android:text="" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/txtPassword"
android:layout_width="wrap_content"
android:layout_weight="1"
android:hint="Password"
android:gravity="center"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:password="true"
/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnLogin"
android:onClick="btnLogin"
android:layout_weight="1"
android:elevation="0dp"
android:text="LOGIN" />
</TableRow>
<TextView android:id="@+id/link_signup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="No account yet? Create one now"
android:gravity="center"
android:textSize="16dip"/>
</TableLayout>
</RelativeLayout>
如果有人能帮助我,我们将非常感激。
答案 0 :(得分:0)
地方
EditText user = (EditText) findViewById(R.id.txtUserName);
EditText password = (EditText) findViewById(R.id.txtPassword);
Button btnLogin = (Button) findViewById(R.id.btnLogin);
String strUser = user.getText().toString();
String strPass = password.getText().toString();
在oncreate()内部
答案 1 :(得分:0)
您错过了支架 在返回x之后添加}