I'm starting to develop on Android and use the IDE Android Studio. I have an app that connects to a database with SQLServer, the only thing it does is make a query that receives 4 fields by sending it 1 ID. The App is already ready and I have finished it in the emulator with the versions of android 5.0, 6.0, 7.0, but with the 4.4 (API 19) when pressing the button that executes the query the application closes.
Connection code:
public Connection conexionBD() {
Connection conexion = null;
try{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
conexion = DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.0.10:1433;databasename=speedex;user=usuario;password=123;");
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
return conexion;
}
Code of inquiry:
public boolean consultarProyecto() {
paquete = (EditText) findViewById(R.id.txtPaquete);
boton = (Button) findViewById(R.id.btnBuscar);
envio = paquete.getText().toString();
boolean resp = false;
try{ //Realizar consulta SELECT
String sql = "SELECT es.estado, en.fechaLlegada, t.transporte, en.precioTotal FROM estado es, envio en, medioTransporte t, paquete p, grupoPaquete g WHERE es.estadoId = en.estadoId AND t.transporteId = en.transporteId AND g.paqueteId = p.paqueteId AND en.grupoId = g.grupoId AND p.paqueteId = ?;";
PreparedStatement cmd = conexionBD().prepareStatement(sql);
//Llenar los parametros de la clase
cmd.setInt(1, Integer.parseInt(envio));
//Ejecutar la consulta
//pedira importar la clase ResultSet
ResultSet rs = cmd.executeQuery();
//Recorrer la lista de registros
if(rs.next()){
resp = true;
//asignandole a los atributos de la clase
estado = rs.getString(1);
fecha = rs.getString(2);
transporte = rs.getString(3);
precio = rs.getDouble(4);
}
//cerrando conexion
cmd.close();
conexionBD().close();
Toast.makeText(getApplicationContext(), "Consulta exitosa", Toast.LENGTH_LONG).show();
}
catch (Exception ex){
Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG).show();
}
return resp;
}
The main activity sends the data to the second Activity:
public void llamarSegundoActiviy(View view) {
consultarProyecto();
System.out.println("El estado es " + estado + "/n" + "La fecha es " + fecha + "/n" + "El tipo de transporte es " + transporte + "/n" + "El precio es " + precio);
Intent intent = new Intent(MainActivity.this, ScrollingActivity.class);
intent.putExtra("ESTADO", estado);
intent.putExtra("FECHA", fecha);
intent.putExtra("TRANSPORTE", transporte);
intent.putExtra("PRECIO", precio);
startActivity(intent);
}
The button has the event OnClick
that calls the previous method.
And this is the code of the second Activity
public class ScrollingActivity extends AppCompatActivity {
TextView estado, fecha, transporte, precio;
Integer numPaquete;
String estado1, fecha1, transporte1;
Double precio1;
DecimalFormat formato = new DecimalFormat("$0.00");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
estado = (TextView) findViewById(R.id.txtEstado);
fecha = (TextView) findViewById(R.id.txtFecha);
transporte = (TextView) findViewById(R.id.txtTransporte);
precio = (TextView) findViewById(R.id.txtPrecio);
Intent inten = getIntent();
Bundle extras = inten.getExtras();
if(extras != null) {
estado1 = extras.getString("ESTADO");
estado.setText(estado1);
fecha1 = extras.getString("FECHA");
fecha.setText(fecha1);
transporte1 = extras.getString("TRANSPORTE");
transporte.setText(transporte1);
precio1 = extras.getDouble("ESTADO");
precio.setText(String.valueOf(formato.format(precio1)));
}
else{
Toast.makeText(getApplicationContext(), "El numero de paquete no existe", Toast.LENGTH_LONG).show();
Intent intent = new Intent(ScrollingActivity.this, MainActivity.class);
startActivity(intent);
}
}
}
What will be the problem if it runs in the other versions of android? Will there be anything else you have to configure?
Update: I add the Manifest code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="andresk21.com.speedex">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ScrollingActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
And the error that the logcat gives me:
09-26 02:55:52.186 2500-2500/andresk21.com.speedex E/AndroidRuntime: FATAL EXCEPTION: main
Process: andresk21.com.speedex, PID: 2500
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.VerifyError: net/sourceforge/jtds/jdbc/TdsCore
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:359)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
at java.sql.DriverManager.getConnection(DriverManager.java:179)
at java.sql.DriverManager.getConnection(DriverManager.java:144)
at andresk21.com.speedex.MainActivity.conexionBD(MainActivity.java:52)
at andresk21.com.speedex.MainActivity.consultarProyecto(MainActivity.java:72)
at andresk21.com.speedex.MainActivity.llamarSegundoActiviy(MainActivity.java:35)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
09-26 02:55:52.186 1561-1715/system_process W/ActivityManager: Force finishing activity andresk21.com.speedex/.MainActivity