title: Android activity error 5
body: <p>Good day for all the guru's of developing here, first of all, sorry for my bad english but I have an error in a Android app that I'am developing and I couldn`t fix it, and I google it and don't find any answer.</p>
  
  <p><strong>The logcat</strong></p>
  
  <pre><code>09-28 13:21:59.360    2085-2085/com.colpsic.domain.colpsic_app I/art﹕ Not late-enabling -Xcheck:jni (already on)
  09-28 13:21:59.360    2085-2085/com.colpsic.domain.colpsic_app I/art﹕ Late-enabling JIT
  09-28 13:21:59.362    2085-2085/com.colpsic.domain.colpsic_app I/art﹕ JIT created with code_cache_capacity=2MB compile_threshold=1000
  09-28 13:21:59.384    2085-2085/com.colpsic.domain.colpsic_app W/System﹕ ClassLoader referenced unknown path: /data/app/com.colpsic.domain.colpsic_app-2/lib/x86
  09-28 13:21:59.387    2085-2085/com.colpsic.domain.colpsic_app D/AndroidRuntime﹕ Shutting down VM
  09-28 13:21:59.387    2085-2085/com.colpsic.domain.colpsic_app E/AndroidRuntime﹕ FATAL EXCEPTION: main
      Process: com.colpsic.domain.colpsic_app, PID: 2085
      java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.colpsic.domain.colpsic_app/com.colpsic.domain.colpsic_app.PrincipalActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' 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.View android.view.Window.findViewById(int)' on a null object reference
              at android.app.Activity.findViewById(Activity.java:2090)
              at com.colpsic.domain.colpsic_app.PrincipalActivity.&lt;init&gt;(PrincipalActivity.java:22)
              at java.lang.Class.newInstance(Native Method)
              at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
              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)
  09-28 13:22:01.785    2085-2085/? I/Process﹕ Sending signal. PID: 2085 SIG: 9
  </code></pre>
  
  <p><strong>PrincipalActivity.java</strong></p>
  
  <pre><code>package com.colpsic.domain.colpsic_app;
  
  import android.content.Intent;
  import android.os.StrictMode;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.ResultSetMetaData;
  import java.sql.Statement;
  
  public class PrincipalActivity extends AppCompatActivity {
      EditText EdtxtCedula=(EditText)findViewById(R.id.lblCedula);
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_principal);
  
          Button btnAceptar=(Button)findViewById(R.id.BtnAceptar);
  
          btnAceptar.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  boolean existeDocumento;
                  existeDocumento = conection();
                  validacionDocumento(existeDocumento);
              }
  
  
          });
      }
  
      private void validacionDocumento(boolean validar) {
          if (validar==true){
              Intent intent = new Intent(PrincipalActivity.this, ProfesionalActivity.class);
              Bundle b = new Bundle();
              b.putInt("CEDULA", Integer.parseInt(EdtxtCedula.getText().toString()));
              intent.putExtras(b);
              startActivity(intent);
          }else{
              EdtxtCedula.setText("");
          }
      }
  
      private boolean conection() {
          final String url = "jdbc:mysql://";
          final String user = "";
          final String pass = "";
          boolean resultadoConexion = false;
          TextView lblCambio=(TextView)findViewById(R.id.lblBienvenido);
          try{
  
              StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
              StrictMode.setThreadPolicy(policy);
  
              Class.forName("com.mysql.jdbc.Driver").newInstance();
              Connection con = DriverManager.getConnection(url, user, pass);
              Statement st = con.createStatement();
              ResultSet rs = st.executeQuery("select * from usuarios where id="+EdtxtCedula.getText());
              //ResultSetMetaData rsmd = rs.getMetaData();
              //rs.next();
              //lblCambio.setText(rs.getString(1));
              while (rs.next()){
                  if ( rs.getRow() == 0 )
                  {
                      //es un result set vacío
                       Toast toast = Toast.makeText(getApplicationContext(), "La cedula no existe", Toast.LENGTH_SHORT);
                       toast.show();
                       resultadoConexion = false;
  
                  }else{
                      //el result set tiene datos
                          //Toast toast = Toast.makeText(getApplicationContext(), "Conexion exitosa", Toast.LENGTH_SHORT);
                          //toast.show();
                          lblCambio.setText(rs.getString(2));
                          resultadoConexion = true;
                  }
              }
              con.close();
  
          } catch (Exception e)
          {
              e.printStackTrace();
          }
          return resultadoConexion;
      }
  
  
  }
  </code></pre>
  
  <p><strong>activity_principal.xml</strong></p>
  
  <pre><code>&lt;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:id="@+id/layContenedor"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".PrincipalActivity"
      android:gravity="center"&gt;
  
      &lt;ImageView  android:id="@+id/imgLogo"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/colpsic_logo"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"
          android:layout_marginBottom="354dp"
          android:contentDescription="@string/logo_descripcion"&gt;
      &lt;/ImageView&gt;
  
      &lt;RelativeLayout   android:id="@+id/layTexto"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_alignTop="@+id/imgLogo"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_marginTop="134dp"&gt;
  
          &lt;TextView  android:id="@+id/lblBienvenido"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textAppearance="?android:attr/textAppearanceLarge"
              android:text="@string/bienvenido"
              android:layout_gravity="center_horizontal"
              android:textSize="32sp"
              android:textColor="#0107ff"
              android:layout_alignParentTop="true"
              android:layout_centerHorizontal="true" /&gt;
  
          &lt;TextView   android:id="@+id/lblMensaje_Bienvenida"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textAppearance="?android:attr/textAppearanceMedium"
              android:text="@string/mensaje"
              android:layout_gravity="center_horizontal"
              android:textColor="#2514ff"
              android:layout_below="@+id/lblBienvenido"
              android:layout_alignParentLeft="true"
              android:layout_alignParentStart="true"
              android:layout_marginTop="39dp" /&gt;
  
          &lt;EditText android:id="@+id/lblCedula"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:hint="@string/cedula"
              android:textColorHint="#CFCFCF"
              android:inputType="number"
              android:layout_below="@+id/lblMensaje_Bienvenida"
              android:layout_alignParentLeft="true"
              android:layout_alignParentStart="true"
              android:layout_marginTop="49dp" /&gt;
      &lt;/RelativeLayout&gt;
  
      &lt;LinearLayout
          android:id="@+id/BarraBotones"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:layout_alignParentBottom="true"
          style="?android:attr/buttonBarStyle"&gt;
  
          &lt;Button android:id="@+id/BtnAceptar"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="@string/Aceptar"
              style="?android:attr/buttonBarButtonStyle" /&gt;
  
          &lt;Button android:id="@+id/BtnCancelar"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="@string/salir"
              style="?android:attr/buttonBarButtonStyle" /&gt;
  
      &lt;/LinearLayout&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>AndroidManifiest.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.colpsic.domain.colpsic_app" &gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name=".PrincipalActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".ProfesionalActivity"
              android:label="Profesional" &gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>