不知道为什么当我在调试模式下运行应用程序时,它会在doInBackground结束时退出应用程序。
以下是代码:
private class AsyncTaskRunner extends AsyncTask<String, String, String>
{
ProgressDialog progressDialog;
@Override
protected String doInBackground(String... params)
{
try
{
Log.e("SQLMainActivity", "Inicio do try");
Connection con;
Statement stmt;
ResultSet rs;
String driver = "net.sourceforge.jtds.jdbc.Driver";
String conString = "jdbc:jtds:sqlserver://" + GlobalVars.SQLServerInfo.get("server") + ":" + GlobalVars.SQLServerInfo.get("port") + "/" + GlobalVars.SQLServerInfo.get("dbname") +
";instance=" + GlobalVars.SQLServerInfo.get("instance");
Class.forName(driver);
Log.e("SQLMainActivity", "Consegui meter a diver como deve ser");
con = DriverManager.getConnection(conString, GlobalVars.SQLServerInfo.get("user"), GlobalVars.SQLServerInfo.get("pass"));
String query = "SELECT * FROM Pda_Documentos ORDER BY Ordem";
stmt = con.createStatement();
rs = stmt.executeQuery(query);
Log.e("SQLMainActivity", "Consegui executar a query como deve ser");
PDADocumentos tempDoc;
// TODO ** Informação ** De lembrar que ao ser adicionados campos aos documentos temos que actualizar esta parte do codigo
while (rs.next())
{
tempDoc = new PDADocumentos();
tempDoc.setID(rs.getString("Tipo"));
tempDoc.setNome(rs.getString("Nome"));
GlobalVars.PDADocInfo.put(rs.getString("Tipo"), tempDoc);
}
stmt.close();
con.close();
Log.e("SQLMainActivity", "Fechei com sucesso a ligação");
SQLConDocuments = true;
}
catch (Exception e)
{
e.printStackTrace();
Log.e("SQLConfig", "Não consegui fazer a ligação");
Log.e("SQLConfig", e.toString());
}
FillDocumentsToMemory();
return "done";
}
@Override
protected void onPostExecute(String result)
{
// execution of result of Long time consuming operation
progressDialog.dismiss();
}
@Override
protected void onPreExecute()
{
progressDialog = ProgressDialog.show(MainActivity.this,
getBaseContext().getResources().getString(R.string.config_connection_title),
getBaseContext().getResources().getString(R.string.config_connection_message));
}
@Override
protected void onProgressUpdate(String... text)
{
progressDialog.setMessage("Messagem enviada com sucesso!");
}
}
在调试过程中,当它到达第34行时按F8(Android Studio);返回&#34;完成&#34;;&#34; doInBackground应用程序关闭,甚至按钮&#34;停止&#34;在android studio上变灰(无法按下,只在程序关闭时才会发生
)BtW忘了添加,我在这里打电话给跑步者:
if (checkFileIfExist("dbHelper.db"))
{
// Metodo Que irá passar PDA_Documentos para o mapa e também actualizar a base de dados SQL
Log.e("AsyncTask","Buscar info principal para andar");
AsyncTaskRunner runner = new AsyncTaskRunner();
runner.execute();
}
else
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.config_sqlitle_message)
.setTitle(R.string.alert_titles_warning)
.setCancelable(false)
.setPositiveButton(R.string.general_ok, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
dialogInterface.cancel();
finishAndRemoveTask();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
要添加的另一件事是,只有在doInBackground中放置断点时才会发生这种情况,如果它没有断点就没有问题