0

我一直在编写一个应用程序,在我想做测试的时候,eclipse Logcat 给了我以下日志,我不知道为什么会这样,手机和模拟器给出“不幸的是你的应用程序已停止”消息:

07-26 19:47:03.880: W/dalvikvm(834): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-26 19:47:03.910: E/AndroidRuntime(834): FATAL EXCEPTION: main
07-26 19:47:03.910: E/AndroidRuntime(834): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.xanmaya.kapschfinal/com.xanmaya.kapschfinal.MainActivity}: java.lang.NullPointerException
07-26 19:47:03.910: E/AndroidRuntime(834):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
07-26 19:47:03.910: E/AndroidRuntime(834):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-26 19:47:03.910: E/AndroidRuntime(834):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-26 19:47:03.910: E/AndroidRuntime(834):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-26 19:47:03.910: E/AndroidRuntime(834):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 19:47:03.910: E/AndroidRuntime(834):  at android.os.Looper.loop(Looper.java:137)
07-26 19:47:03.910: E/AndroidRuntime(834):  at android.app.ActivityThread.main(ActivityThread.java:5041)
07-26 19:47:03.910: E/AndroidRuntime(834):  at java.lang.reflect.Method.invokeNative(Native Method)
07-26 19:47:03.910: E/AndroidRuntime(834):  at java.lang.reflect.Method.invoke(Method.java:511)
07-26 19:47:03.910: E/AndroidRuntime(834):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-26 19:47:03.910: E/AndroidRuntime(834):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-26 19:47:03.910: E/AndroidRuntime(834):  at dalvik.system.NativeStart.main(Native Method)
07-26 19:47:03.910: E/AndroidRuntime(834): Caused by: java.lang.NullPointerException
07-26 19:47:03.910: E/AndroidRuntime(834):  at android.app.Activity.findViewById(Activity.java:1839)
07-26 19:47:03.910: E/AndroidRuntime(834):  at com.xanmaya.kapschfinal.MainActivity.<init>(MainActivity.java:36)
07-26 19:47:03.910: E/AndroidRuntime(834):  at java.lang.Class.newInstanceImpl(Native Method)
07-26 19:47:03.910: E/AndroidRuntime(834):  at java.lang.Class.newInstance(Class.java:1319)
07-26 19:47:03.910: E/AndroidRuntime(834):  at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
07-26 19:47:03.910: E/AndroidRuntime(834):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-26 19:47:03.910: E/AndroidRuntime(834):  ... 11 more

这是代码:

 package com.xanmaya.kapschfinal;

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

public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);


Button btnEnviar = (Button) findViewById(R.id.btnEnviar);
btnEnviar.setOnClickListener(new View.OnClickListener() { 
@Override
public void onClick(View v) {
    }}
);
return true;

}
EditText etModulo = (EditText) findViewById(R.id.etModulo);
EditText etAmpli = (EditText) findViewById(R.id.etAmpli);
EditText etAntena = (EditText) findViewById(R.id.etAntena);
EditText etCable = (EditText) findViewById(R.id.etCable);
EditText etFecha = (EditText) findViewById(R.id.etFecha);
EditText etlugar = (EditText) findViewById(R.id.etlugar);
EditText etenvia = (EditText) findViewById(R.id.etenvia);
CheckBox checkampli = (CheckBox) findViewById(R.id.checkampli);
CheckBox checkantena = (CheckBox) findViewById(R.id.checkantena);
CheckBox checkmodulo = (CheckBox) findViewById(R.id.checkmodulo);
CheckBox checkcable = (CheckBox) findViewById(R.id.checkcable);

{
    Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.putExtra(Intent.EXTRA_EMAIL, "villasantdesign@gmail.com");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Resumen");
emailIntent.putExtra(Intent.EXTRA_TEXT, etModulo.getText()); etAntena.getText(); etCable.getText(); etlugar.getText(); etFecha.getText(); etAmpli.getText();}{;
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(emailIntent, "btnEnviar"));}}`code:

非常感谢您的帮助!谢谢!

4

4 回答 4

1

伙计,onCreate在调用 setContentView 之后调用所有视图初始化。类成员初始化在调用隐式或不调用后立即在构造函数中调用super (...)。您的视图稍后在 onCreate 中创建。

于 2013-07-26T23:38:47.600 回答
0

日志中要关注的行是

    07-26 19:47:03.910: E/AndroidRuntime(834): Caused by: java.lang.NullPointerException
    07-26 19:47:03.910: E/AndroidRuntime(834):  at android.app.Activity.findViewById(Activity.java:1839)
    07-26 19:47:03.910: E/AndroidRuntime(834):  at com.xanmaya.kapschfinal.MainActivity.<init>(MainActivity.java:36)

findViewById() 在 MainActivity 的第 36 行找不到具有指定视图的视图。检查此视图是否存在于您的 xml 布局中。

于 2013-07-26T23:39:13.953 回答
0

很难以格式化的方式阅读您的代码,但似乎这些行

EditText etModulo = (EditText) findViewById(R.id.etModulo);
EditText etAmpli = (EditText) findViewById(R.id.etAmpli);
EditText etAntena = (EditText) findViewById(R.id.etAntena);
EditText etCable = (EditText) findViewById(R.id.etCable);
EditText etFecha = (EditText) findViewById(R.id.etFecha);
EditText etlugar = (EditText) findViewById(R.id.etlugar);
EditText etenvia = (EditText) findViewById(R.id.etenvia);
CheckBox checkampli = (CheckBox) findViewById(R.id.checkampli);
CheckBox checkantena = (CheckBox) findViewById(R.id.checkantena);
CheckBox checkmodulo = (CheckBox) findViewById(R.id.checkmodulo);
CheckBox checkcable = (CheckBox) findViewById(R.id.checkcable);

不在任何方法之外,因此它们会在其他任何方法之前被调用,特别是onCreate()在您调用setContentView()它们的地方null

Views存在于您的布局中,因此null如果您在通过调用扩展布局之前尝试初始化它们,它们将返回setContentView()。一种更传统的方式,更易读的方式是将它们声明为成员变量,然后onCreate()如果您选择 AFTER the all to ,则在初始化方法中或初始化方法中初始化它们setContentView()。所以它可能看起来像

public class MainActivity extends Activity {

    EditText etModulo;   // you can declare them as member variables here so they can be used anywhere in the class
    EditText etAmpli; 
    EditText etAntena; 

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

    etModulo = (EditText) findViewById(R.id.etModulo);   // but initialize them here after inflating your layout
    etAmpli = (EditText) findViewById(R.id.etAmpli);
    etAntena = (EditText) findViewById(R.id.etAntena);

}
于 2013-07-26T23:41:36.833 回答
0

首先,这段代码甚至可以编译吗?或者你只是把它贴错了?

1)你的findViews应该在你的setContentView()之后进入onCreate。

所以在你的 onCreate 中添加这个

EditText etModulo = (EditText) findViewById(R.id.etModulo);
EditText etAmpli = (EditText) findViewById(R.id.etAmpli);
EditText etAntena = (EditText) findViewById(R.id.etAntena);
EditText etCable = (EditText) findViewById(R.id.etCable);
EditText etFecha = (EditText) findViewById(R.id.etFecha);
EditText etlugar = (EditText) findViewById(R.id.etlugar);
EditText etenvia = (EditText) findViewById(R.id.etenvia);
CheckBox checkampli = (CheckBox) findViewById(R.id.checkampli);
CheckBox checkantena = (CheckBox) findViewById(R.id.checkantena);
CheckBox checkmodulo = (CheckBox) findViewById(R.id.checkmodulo);
CheckBox checkcable = (CheckBox) findViewById(R.id.checkcable);

2)此外,这段代码在不知不觉中在做什么?

{
    Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
    emailIntent.setData(Uri.parse("mailto:"));
    emailIntent.putExtra(Intent.EXTRA_EMAIL, "villasantdesign@gmail.com");
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Resumen");
    emailIntent.putExtra(Intent.EXTRA_TEXT, etModulo.getText()); etAntena.getText(); etCable.getText(); etlugar.getText(); etFecha.getText(); etAmpli.getText();}{;
    Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
    emailIntent.setType("message/rfc822");
    startActivity(Intent.createChooser(emailIntent, "btnEnviar"));}}:

将此添加到您的 btnEnviar 的 onClick 中。

于 2013-07-26T23:50:19.353 回答