-3

I'm just starting to use Android Studio and allready having trouble, I need to conect to a database on sqlserver via my cellphone, but it crashes before the whole thing starts to work, I need a little help please, here is the code:

  import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class Farmaciapp extends AppCompatActivity {
    EditText edtMed =(EditText)findViewById(R.id.edtMedicamento);
    String ConsulMed = edtMed.getText().toString();
    public void Meds (View v){
        Connection conexion = null;
        String url = "jdbc:jtds:sqlserver://192.168.1.69;databaseName=Medicamentos;";
        String query = "Select Descripcion from Medicina where Nombre = '" + ConsulMed + "'";
        try {
            Connection con = DriverManager.getConnection(url);
            Statement state = con.createStatement();
            ResultSet resultado = state.executeQuery(query);
            while (resultado.next()){
                String res = resultado.getString(1);
                TextView Explicacion = (TextView) findViewById(R.id.mltExplic);
                Explicacion.setText(res);
            }
        }catch (Exception e){
            Toast.makeText(getApplicationContext(), e.getMessage() , Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_farmaciapp);
    }
}

2 Answers2

1

You need to Do findViewById inside onCreate() method

Declare your EditText edtMed as global

 EditText edtMed;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_farmaciapp);

         edtMed =(EditText)findViewById(R.id.edtMedicamento);

    }

Also get text from your EditText inside Meds (View v)

public void Meds (View v){

         String ConsulMed = edtMed.getText().toString();
        Connection conexion = null;
        String url = "jdbc:jtds:sqlserver://192.168.1.69;databaseName=Medicamentos;";
        String query = "Select Descripcion from Medicina where Nombre = '" + ConsulMed + "'";
        try {
            Connection con = DriverManager.getConnection(url);
            Statement state = con.createStatement();
            ResultSet resultado = state.executeQuery(query);
            while (resultado.next()){
                String res = resultado.getString(1);
                TextView Explicacion = (TextView) findViewById(R.id.mltExplic);
                Explicacion.setText(res);
            }
        }catch (Exception e){
            Toast.makeText(getApplicationContext(), e.getMessage() , Toast.LENGTH_SHORT).show();
        }

    }
Goku
  • 9,102
  • 8
  • 50
  • 81
1

The reason is you are trying to access which haven't yet created. Or in another word we can't reference or call findViewById until the layout is inflated.

so make below changes

  public class Farmaciapp extends AppCompatActivity {

    EditText edtMed;
    String ConsulMed = edtMed.getText().toString();

    public void Meds (View v){
        Connection conexion = null;
        String url = "jdbc:jtds:sqlserver://192.168.1.69;databaseName=Medicamentos;";
        String query = "Select Descripcion from Medicina where Nombre = '" + ConsulMed + "'";
        try {
            Connection con = DriverManager.getConnection(url);
            Statement state = con.createStatement();
            ResultSet resultado = state.executeQuery(query);
            while (resultado.next()){
                String res = resultado.getString(1);
                TextView Explicacion = (TextView) findViewById(R.id.mltExplic);
                Explicacion.setText(res);
            }
        }catch (Exception e){
            Toast.makeText(getApplicationContext(), e.getMessage() , Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_farmaciapp);
        edtMed =(EditText)findViewById(R.id.edtMedicamento);
    }
}

make sure to access edtMed after onCreate not before that will work. onCreate() is the way for activity to inflate the layout.

vikas kumar
  • 10,447
  • 2
  • 46
  • 52