致命异常主要在 Android 上运行应用程序?
Posted
技术标签:
【中文标题】致命异常主要在 Android 上运行应用程序?【英文标题】:Fatal Exception Main running Application on Android? 【发布时间】:2013-07-26 23:35:11 【问题描述】:我一直在编写一个应用程序,当时我想做测试,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 ComponentInfocom.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:
非常感谢您的帮助!谢谢!
【问题讨论】:
【参考方案1】:老兄,在调用 setContentView 之后,在 onCreate
中调用所有视图初始化。在调用隐式或不调用 super (...)
之后,立即在构造函数中调用类成员初始化。您的视图稍后在 onCreate 中创建。
【讨论】:
【参考方案2】:首先,这段代码是否可以编译?还是你刚才贴错了?
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 中添加这个。
【讨论】:
【参考方案3】:日志中要关注的行是
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 布局中。
【讨论】:
【参考方案4】:很难阅读您的代码格式化方式,但这些行似乎
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
存在于您的布局中,因此如果您尝试在通过调用setContentView()
扩展布局之前尝试初始化它们,它们将返回null
。一种更传统的方式,更易读的方式是将它们声明为成员变量,然后在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);
【讨论】:
以上是关于致命异常主要在 Android 上运行应用程序?的主要内容,如果未能解决你的问题,请参考以下文章
致命异常:主要 java.lang.NoClassDefFoundError:android.support.v7.internal.widget.TintManager
致命异常:android.app.RemoteServiceException:无法在 android.os.Handler.dispatchMessage 传送广播