我已经制作了一个应用程序,但当时我想测试它,它会给出消息the application has stopped unexpectedly. please try again
。它似乎在第68行有一个错误,但我找不到它。有什么问题?
下面是代码:
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"));
}}}
以下是我尝试启动应用程序时Logcat吉普车给出的错误日志:
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
答案 0 :(得分:4)
您尝试在setText())
上调用方法vwfecha1
,但尚未对其进行初始化,因此它为null
。更改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);
此外,我不确定onClick()
中的这一行会给出
v.getId()==findViewById(R.id.enviar).getId()
但它可以简单地
v.getId()==R.id.enviar
看起来它会做同样的事情,但你正在采取额外的,不必要的步骤