Question

I've made an application but at the time I want to test it, it gives the message the application has stopped unexpectedly. please try again. It seems that it has an error in line 68, but I can't find it. What is wrong?

Heres the code:

package com.example.kapscht;

import java.sql.Date;
import java.text.DateFormat;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener{
CheckBox checkgrua; 
CheckBox checkreader1; 
CheckBox checkreader2;
CheckBox checkctm1;
CheckBox checkctm2; 
CheckBox checkpsm1; 
CheckBox checkpsm2;
CheckBox checklbm1;
CheckBox checklbm2;
CheckBox checkrx1;
CheckBox checkrx2; 
CheckBox checktx1; 
CheckBox checktx2;
CheckBox checkmodulerf1;
CheckBox checkmodulerf2; 
CheckBox checkmodulemrf1;
CheckBox checkmodulemrf2;
CheckBox checkcablerf1; 
CheckBox checkcablerf2;
CheckBox checkconectorrf1;
CheckBox checkconectorrf2;
CheckBox checkatenuacionrf1;
CheckBox checkatenuacionrf2; 
CheckBox checkloopback1; 
CheckBox checkloopback2;
CheckBox checkfirmware1;
CheckBox checkfirmware2; 
CheckBox checkethernet1; 
CheckBox checkethernet2;
CheckBox checkamplificador1;
CheckBox checkamplificador2;
CheckBox checkfp1;
CheckBox checkfp2; 
CheckBox checksincro1; 
CheckBox checksincro2;
CheckBox checkotro1;
CheckBox checkotro2; 
EditText etsitio; 
EditText etfalla;
EditText etdescripcion;
EditText etqg;
TextView vwfecha1; 
TextView vwfecha2; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Date date = new Date(0);
    String dateTime = DateFormat.getDateTimeInstance().format(date);
    vwfecha1.setText(dateTime);
    checkgrua= (CheckBox) findViewById(R.id.checkgrua);
    checkreader1= (CheckBox) findViewById(R.id.checkreader1);
    checkreader2= (CheckBox) findViewById(R.id.checkreader2);
    checkctm1= (CheckBox) findViewById(R.id.checkctm1);
    checkctm2= (CheckBox) findViewById(R.id.checkctm2);
    checkpsm1= (CheckBox) findViewById(R.id.checkpsm1);
    checkpsm2= (CheckBox) findViewById(R.id.checkpsm2);
    checklbm1= (CheckBox) findViewById(R.id.checklbm1);
    checklbm2= (CheckBox) findViewById(R.id.checklbm2);
    checkrx1= (CheckBox) findViewById(R.id.checkrx1);
    checkrx2= (CheckBox) findViewById(R.id.checkrx2);
    checktx1= (CheckBox) findViewById(R.id.checktx1);
    checktx2= (CheckBox) findViewById(R.id.checktx2);
    checkmodulerf1= (CheckBox) findViewById(R.id.checkmodulerf1);
    checkmodulerf2= (CheckBox) findViewById(R.id.checkmodulerf2);
    checkmodulemrf1= (CheckBox) findViewById(R.id.checkmodulemrf1);
    checkmodulemrf2= (CheckBox) findViewById(R.id.checkmodulemrf2);
    checkcablerf1= (CheckBox) findViewById(R.id.checkcablerf1);
    checkcablerf2= (CheckBox) findViewById(R.id.checkcablerf2);
    checkconectorrf1= (CheckBox) findViewById(R.id.checkconectorrf1);
    checkconectorrf2= (CheckBox) findViewById(R.id.checkconectorrf2);
    checkatenuacionrf1= (CheckBox) findViewById(R.id.checkatenuacionrf1);
    checkatenuacionrf2= (CheckBox) findViewById(R.id.checkatenuacionrf2);
    checkloopback1= (CheckBox) findViewById(R.id.checkloopback1);
    checkloopback2= (CheckBox) findViewById(R.id.checkloopback2);
    checkfirmware1= (CheckBox) findViewById(R.id.checkfirmware1);
    checkfirmware2= (CheckBox) findViewById(R.id.checkfirmware2);
    checkethernet1= (CheckBox) findViewById(R.id.checkethernet1);
    checkethernet2= (CheckBox) findViewById(R.id.checkethernet2);
    checkamplificador1= (CheckBox) findViewById(R.id.checkamplificador1);
    checkamplificador2= (CheckBox) findViewById(R.id.checkamplificador2);
    checkfp1= (CheckBox) findViewById(R.id.checkfp1);
    checkfp2= (CheckBox) findViewById(R.id.checkfp2);
    checksincro1= (CheckBox) findViewById(R.id.checksincro1);
    checksincro2= (CheckBox) findViewById(R.id.checksincro2);
    checkotro1= (CheckBox) findViewById(R.id.checkotro1);
    checkotro2= (CheckBox) findViewById(R.id.checkotro2);
    etsitio= (EditText) findViewById(R.id.etsitio);
    etfalla= (EditText) findViewById(R.id.etfalla); 
    etdescripcion= (EditText) findViewById(R.id.etdescripcion);
    etqg= (EditText) findViewById(R.id.etqg);
    View boton = (Button) findViewById(R.id.enviar);
    boton.setOnClickListener(this); 

}

@Override
public void onClick(View v) {
    if (v.getId()==findViewById(R.id.enviar).getId())
    {
        Intent intentEmail = new Intent(Intent.ACTION_SEND, Uri.fromParts("mailto", "villasantdesign@gmail.com", null));
        intentEmail.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"villasantdesign@gmail.com"});
        intentEmail.putExtra(Intent.EXTRA_SUBJECT, "Consulta Técnica");
        intentEmail.putExtra(Intent.EXTRA_TEXT, "Fecha/hora abierto: "+vwfecha1.getText().toString()+"\nSitio:  "+etsitio.getText()+"\nSe usó grua:  "+checkgrua.isChecked()+"\nFalla Reportada:  "+etfalla.getText()+"\nSe reemplazó reader/lector: "+checkreader1.isChecked()+"\nSe ajustó reader/lector: "+checkreader2.isChecked()+"\nSe reemplazó CTM:  "+checkctm1.isChecked()+"\nSe ajustó CTM:  "+checkctm2.isChecked()+"\nSe reemplazó PSM: "+checkpsm1.isChecked()+"\nSe ajustó PSM: "+checkpsm2.isChecked()+"\nSe reemplazó LBM:  "+checklbm1.isChecked()+"\nSe ajustó LBM:  "+checklbm2.isChecked()+"\nSe reemplazó Antena Rx: "+checkrx1.isChecked()+"\nSe ajustó Antena Rx: "+checkrx2.isChecked()+"\nSe reemplazó Antena Tx:  "+checktx1.isChecked()+"\nSe ajustó Antena Tx:  "+checktx2.isChecked()+"\nSe reemplazó Módulo RF: "+checkmodulerf1.isChecked()+"\nSe ajustó Módulo RF: "+checkmodulerf2.isChecked()+"\nSe reemplazó Módulo MRF:  "+checkmodulemrf1.isChecked()+"\nSe ajustó Módulo MRF:  "+checkmodulemrf2.isChecked()+"\nSe reemplazó Cable/Jumper RF: "+checkcablerf1.isChecked()+"\nSe ajustó Cable/Jumper RF: "+checkcablerf2.isChecked()+"\nSe reemplazó Conector RF:  "+checkconectorrf1.isChecked()+"\nSe ajustó Conector RF:  "+checkconectorrf2.isChecked()+"\nSe reemplazó Atenuación RF: "+checkatenuacionrf1.isChecked()+"\nSe ajustó Atenuación RF: "+checkatenuacionrf2.isChecked()+"\nSe reemplazó Cable Loopback RF: "+checkloopback1.isChecked()+"\nSe ajustó Cable Loopback RF: "+checkloopback2.isChecked()+"\nSe reemplazó SW/Firmware: "+checkfirmware1.isChecked()+"\nSe ajustó SW/Firmware: "+checkfirmware2.isChecked()+"\nSe reemplazó Conexión Ethernet:  "+checkethernet1.isChecked()+"\nSe ajustó Conexión Ethernet:  "+checkethernet2.isChecked()+"\nSe reemplazó Amplificador: "+checkamplificador1.isChecked()+"\nSe ajustó Amplificador: "+checkamplificador2.isChecked()+"\nSe reemplazó FP Amp.:  "+checkfp1.isChecked()+"\nSe ajustó FP Amp.:  "+checkfp2.isChecked()+"\nSe reemplazó Sincronización: "+checksincro1.isChecked()+"\nSe ajustó Sincronización: "+checksincro2.isChecked()+"\nSe reemplazó OTRO:  "+checkotro1.isChecked()+"\nSe ajustó OTRO:  "+checkotro2.isChecked());         
        intentEmail.setType("message/rfc822");
        startActivity(Intent.createChooser(intentEmail, "Enviar"));         
        }}}

And here is the error log Logcat jeeps giving me when I try to launch my application:

07-29 12:37:35.269: W/dalvikvm(10939): threadid=1: thread exiting with uncaught exception (group=0x4001e560)
07-29 12:37:35.289: E/AndroidRuntime(10939): FATAL EXCEPTION: main
07-29 12:37:35.289: E/AndroidRuntime(10939): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kapscht/com.example.kapscht.MainActivity}: java.lang.NullPointerException
07-29 12:37:35.289: E/AndroidRuntime(10939):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1702)
07-29 12:37:35.289: E/AndroidRuntime(10939):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1727)
07-29 12:37:35.289: E/AndroidRuntime(10939):    at android.app.ActivityThread.access$1500(ActivityThread.java:124)
07-29 12:37:35.289: E/AndroidRuntime(10939):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:974)
07-29 12:37:35.289: E/AndroidRuntime(10939):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 12:37:35.289: E/AndroidRuntime(10939):    at android.os.Looper.loop(Looper.java:130)
07-29 12:37:35.289: E/AndroidRuntime(10939):    at android.app.ActivityThread.main(ActivityThread.java:3859)
07-29 12:37:35.289: E/AndroidRuntime(10939):    at java.lang.reflect.Method.invokeNative(Native Method)
07-29 12:37:35.289: E/AndroidRuntime(10939):    at java.lang.reflect.Method.invoke(Method.java:507)
07-29 12:37:35.289: E/AndroidRuntime(10939):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:890)
07-29 12:37:35.289: E/AndroidRuntime(10939):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:648)
07-29 12:37:35.289: E/AndroidRuntime(10939):    at dalvik.system.NativeStart.main(Native Method)
07-29 12:37:35.289: E/AndroidRuntime(10939): Caused by: java.lang.NullPointerException
07-29 12:37:35.289: E/AndroidRuntime(10939):    at com.example.kapscht.MainActivity.onCreate(MainActivity.java:68)
07-29 12:37:35.289: E/AndroidRuntime(10939):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)   
07-29 12:37:35.289: E/AndroidRuntime(10939):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1666)
07-29 12:37:35.289: E/AndroidRuntime(10939):    ... 11 more
Was it helpful?

Solution

You try to call a method (setText()), on vwfecha1 but you haven't initialized it yet so it is null. Change your onCreate()

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Date date = new Date(0);
String dateTime = DateFormat.getDateTimeInstance().format(date);

vwfecha1 = (TextView) findViewById(R.id.idOfvwfecha1);  // add this line with the appropriate id
vwfecha1.setText(dateTime);

Also, I'm not sure what this line in your onClick() will give

v.getId()==findViewById(R.id.enviar).getId()

but it can simply be

v.getId()==R.id.enviar

It looks like it will do the same thing but you are taking extra, unnecessary steps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top