2013-07-29 160 views
-1

我已经提出申请,但当时我想测试它,它给出了消息the application has stopped unexpectedly. please try again。它似乎在第68行有错误,但我找不到它。哪里不对?应用程序无法启动android?

继承人的代码:

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", "[email protected]", null)); 
     intentEmail.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
     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 

回答

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 

它看起来像它会做同样的事情,但你正在服用额外的,不必要的步骤

+0

@ 323go感谢编辑,但“()”是有意的。我喜欢把这样的东西放在一起......当我比较'switch'中的'id's时,我会做同样的事情 – codeMagic

+0

您知道我如何生成退出时间和日期吗?第一个变量是生成进入应用程序的时间,我想要另一个日期来生成离开应用程序的时间。 – Miguel

+0

这只是取决于您对“退出”的定义,我猜想以及您如何处理您的应用生命周期。如果他们按下“Home”键,那么这是否值得?如果是这样,您将需要在每个“活动”中为主键按下监听器。如果他们退出,那么永远不要完成你的第一个“活动”,直到他们按下后退按钮并在那里获得时间。 – codeMagic