致命异常:Android 应用程序运行时的主程序
Posted
技术标签:
【中文标题】致命异常:Android 应用程序运行时的主程序【英文标题】:FATAL EXCEPTION: main while Android application running 【发布时间】:2015-11-24 18:00:53 【问题描述】:我是一个完全的新手,我现在正在学习制作 android 应用程序。使用了其中一个教程。一切都很好,但由于某些原因,我的应用程序每次启动时都会崩溃。
我真的很努力地想在 LogCat 中看到原因,但我没能做到。如果有人告诉我发生了什么,我将不胜感激。
这里是 LogCat:
08-30 19:13:21.955: W/dalvikvm(7272): threadid=1: thread exiting with uncaught exception (group=0x41869d58)
08-30 19:13:21.955: E/AndroidRuntime(7272): FATAL EXCEPTION: main
08-30 19:13:21.955: E/AndroidRuntime(7272): Process: alesto.androidinterview, PID: 7272
08-30 19:13:21.955: E/AndroidRuntime(7272): java.lang.RuntimeException: Unable to start activity ComponentInfoalesto.androidinterview/alesto.androidinterview.SimpleQuestion: java.lang.NullPointerException
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2259)
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.app.ActivityThread.access$800(ActivityThread.java:149)
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.os.Handler.dispatchMessage(Handler.java:102)
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.os.Looper.loop(Looper.java:136)
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.app.ActivityThread.main(ActivityThread.java:5110)
08-30 19:13:21.955: E/AndroidRuntime(7272): at java.lang.reflect.Method.invokeNative(Native Method)
08-30 19:13:21.955: E/AndroidRuntime(7272): at java.lang.reflect.Method.invoke(Method.java:515)
08-30 19:13:21.955: E/AndroidRuntime(7272): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
08-30 19:13:21.955: E/AndroidRuntime(7272): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-30 19:13:21.955: E/AndroidRuntime(7272): at dalvik.system.NativeStart.main(Native Method)
08-30 19:13:21.955: E/AndroidRuntime(7272): Caused by: java.lang.NullPointerException
08-30 19:13:21.955: E/AndroidRuntime(7272): at alesto.androidinterview.SimpleQuestion.onCreate(SimpleQuestion.java:56)
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.app.Activity.performCreate(Activity.java:5231)
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-30 19:13:21.955: E/AndroidRuntime(7272): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
08-30 19:13:21.955: E/AndroidRuntime(7272): ... 11 more
08-30 19:13:21.965: V/ActivityThread(7272): SCHEDULE 102 PAUSE_ACTIVITY_FINISHING: 0 / android.os.BinderProxy@429157e0
08-30 19:13:22.535: V/ActivityThread(7272): SCHEDULE 109 DESTROY_ACTIVITY: 1 / android.os.BinderProxy@428cbe18
08-30 19:13:22.535: V/ActivityThread(7272): SCHEDULE 140 TRIM_MEMORY: 20 / null
08-30 19:13:22.535: V/ActivityThread(7272): SCHEDULE 109 DESTROY_ACTIVITY: 1 / android.os.BinderProxy@429157e0
这里是首页活动:
package alesto.androidinterview;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.app.ActionBar;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class FrontPage extends ActionBarActivity implements OnClickListener
Button bsimple, btough, bseeapps, brateapp;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.front_page);
//initializing buttons
bsimple = (Button) findViewById(R.id.bsq);
btough = (Button) findViewById(R.id.btq);
bseeapps = (Button) findViewById(R.id.bseeotherapps);
brateapp = (Button) findViewById(R.id.brateapp);
bsimple.setOnClickListener(this);
btough.setOnClickListener(this);
bseeapps.setOnClickListener(this);
brateapp.setOnClickListener(this);
//adding action bar
LinearLayout frontLL = (LinearLayout) findViewById(R.id.front_page_titlebar);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.front_page_title_bar);
@Override
public void onClick(View v)
// TODO Auto-generated method stub
switch (v.getId())
case R.id.bsq:
Intent i = new Intent(FrontPage.this, SimpleQuestion.class);
startActivity(i);
break;
case R.id.btq:
Intent j = new Intent(FrontPage.this, ToughQuestion.class);
startActivity(j);
break;
case R.id.bseeotherapps:
break;
case R.id.brateapp:
Uri uri1 = Uri.parse("market://details?id="+getPackageName());
Intent goToMarket1 = new Intent(Intent.ACTION_VIEW, uri1);
startActivity(goToMarket1);
break;
及其'XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/bgfirst"
android:id="@+id/frontpagelayout" >
<Button
android:id="@+id/bsq"
android:layout_
android:layout_
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:background="@drawable/button_custom"
android:textColor="#050000"
android:text="@string/sq" />
<Button
android:id="@+id/btq"
android:layout_
android:layout_
android:textSize="20sp"
android:layout_below="@id/bsq"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:background="@drawable/button_custom"
android:textColor="#050000"
android:text="@string/tq" />
<Button
android:id="@+id/bseeotherapps"
android:layout_
android:layout_
android:textSize="20sp"
android:layout_below="@id/btq"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:background="@drawable/button_custom"
android:textColor="#050000"
android:text="@string/soa" />
<Button
android:id="@+id/brateapp"
android:layout_
android:layout_
android:textSize="20sp"
android:layout_below="@id/bseeotherapps"
android:layout_centerHorizontal="true"
android:layout_marginTop="49dp"
android:background="@drawable/button_custom"
android:textColor="#050000"
android:text="@string/ra" />
</RelativeLayout>
目标活动:
package alesto.androidinterview;
import java.util.Locale;
import android.app.ActionBar;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class SimpleQuestion extends ActionBarActivity implements
OnClickListener
TextView tvQuestion, tvAnswer, tvTotalLength, tvPresentIndex;
Button bleft, bright, bshow;
String[] simpleQuestions, simpleAnswers;
int index;
public static final String defaultAnswer = "Press \"A\" for the answer";
// variables and object of text-to-speech
TextToSpeech ttsobject;
int result;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.questions);
// initializiation of textviews and buttons
tvQuestion = (TextView) findViewById(R.id.tvquestion);
tvAnswer = (TextView) findViewById(R.id.tvanswer);
tvTotalLength = (TextView) findViewById(R.id.tvyy);
tvPresentIndex = (TextView) findViewById(R.id.tvxx);
bleft = (Button) findViewById(R.id.bleft);
bright = (Button) findViewById(R.id.bright);
bshow = (Button) findViewById(R.id.bshowanswer);
Button bspeak = (Button) findViewById(R.id.bspeak);
Button bstop = (Button) findViewById(R.id.bstop_mute);
// setting onclick listeners to 3 buttons and SPEAK/MUTE buttons
bleft.setOnClickListener(this);
bright.setOnClickListener(this);
bshow.setOnClickListener(this);
bspeak.setOnClickListener(this);
bstop.setOnClickListener(this);
// importing string arrays
simpleQuestions = getResources().getStringArray(R.array.simple_ques);
simpleAnswers = getResources().getStringArray(R.array.simple_ans);
// setting values to different variables
index = 0;
tvQuestion.setText(simpleQuestions[index]);
tvAnswer.setText(defaultAnswer);
tvPresentIndex.setText(String.valueOf(index + 1));
tvTotalLength.setText("/" + String.valueOf(simpleQuestions.length));
// adding action bar and customizing it
LinearLayout questionLL = (LinearLayout) findViewById(R.id.question_page_titlebar);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.questions_title_bar);
TextView category = (TextView) findViewById(R.id.tv_questions_titlebar);
category.setText("Simple questions");
// initialization of Text-to-Speech object
ttsobject = new TextToSpeech(SimpleQuestion.this,
new TextToSpeech.OnInitListener()
@Override
public void onInit(int status)
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS)
ttsobject.setLanguage(Locale.ENGLISH);
else
Toast.makeText(getApplicationContext(),
"Feature is not supported in your device",
Toast.LENGTH_SHORT).show();
);
@Override
public void onClick(View v)
// TODO Auto-generated method stub
switch (v.getId())
case R.id.bleft:
bshow.setActivated(false);
tvAnswer.setText(defaultAnswer);
index--;
if (index == -1)
index = simpleQuestions.length - 1;
tvQuestion.setText(simpleQuestions[index]);
tvPresentIndex.setText(String.valueOf(index + 1));
break;
case R.id.bright:
bshow.setActivated(false);
tvAnswer.setText(defaultAnswer);
index++;
if (index == simpleQuestions.length)
index = 0;
tvQuestion.setText(simpleQuestions[index]);
tvPresentIndex.setText(String.valueOf(index + 1));
break;
case R.id.bshowanswer:
tvAnswer.setText(simpleAnswers[index]);
bshow.setActivated(true);
break;
case R.id.bspeak:
if (result == TextToSpeech.LANG_NOT_SUPPORTED
|| result == TextToSpeech.LANG_MISSING_DATA)
Toast.makeText(getApplicationContext(),
"Feature is not supported in your device",
Toast.LENGTH_SHORT).show();
else
if (!tvAnswer.getText().toString().equals(defaultAnswer))
ttsobject.speak(simpleAnswers[index],
TextToSpeech.QUEUE_FLUSH, null);
break;
case R.id.bstop_mute:
if (ttsobject != null)
ttsobject.stop();
break;
protected void onDestroy()
// TODO Auto-generated method stub
super.onDestroy();
if (ttsobject != null)
ttsobject.stop();
ttsobject.shutdown();
目标XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical"
android:weightSum="100">
<LinearLayout
android:layout_
android:layout_
android:layout_weight="8"
android:gravity="center"
android:background="@drawable/bg_topbottom" >
<TextView
android:id="@+id/tvxx"
android:layout_
android:layout_
android:text="@string/tv_xx"
android:textColor="#000000" />
<TextView
android:id="@+id/tvyy"
android:layout_
android:layout_
android:text="@string/tv_yy"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical"
android:layout_weight="80"
android:background="@drawable/main_background" >
<TextView
android:id="@+id/tvquestion"
android:layout_
android:layout_
android:padding="18dp"
android:text="@string/tv_question"
android:textColor="#000000" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_
android:layout_ >
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical" >
<TextView
android:id="@+id/tvanswer"
android:layout_
android:layout_
android:padding="15dp"
android:text="@string/tv_answer"
android:textColor="#000000" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:layout_weight="12"
android:gravity="center_horizontal|center"
android:background="@drawable/bg_topbottom" >
<Button
android:id="@+id/bleft"
android:layout_
android:layout_
android:background="@drawable/left_button" />
<Button
android:id="@+id/bshowanswer"
android:layout_
android:layout_
android:background="@drawable/a_button" />
<Button
android:id="@+id/bright"
android:layout_
android:layout_
android:background="@drawable/right_button" />
</LinearLayout>
</LinearLayout>
在 Eclipse 中尝试过 Clean 功能。不幸的是没有帮助。
【问题讨论】:
SimpleQuestion.java
的第 56 行是哪一行?
【参考方案1】:
在您的 logcat 中指定了基础错误:
08-30 19:13:21.955:E/AndroidRuntime(7272):原因:java.lang.NullPointerException 08-30 19:13:21.955: E/AndroidRuntime(7272): at alesto.androidinterview.SimpleQuestion.onCreate(SimpleQuestion.java:56)
第 56 行是:
bspeak.setOnClickListener(this);
所以bspeak
为空。 bspeak
被初始化为
Button bspeak = (Button) findViewById(R.id.bspeak);
这意味着视图不包含 ID 为 R.id.bspeak
的视图。您应该创建缺少的视图 bspeak
或删除引用它的代码。
【讨论】:
【参考方案2】:您好,bspeak
和 bstop_mute
这两个按钮没有 xlm,也就是说,您正在通过 id 获得 java.lang.NullPointerException
android 所有方式。
Button bspeak = (Button) findViewById(R.id.bspeak);
Button bstop = (Button) findViewById(R.id.bstop_mute);
请为此 ID 创建一个按钮,然后您就可以开始了
【讨论】:
【参考方案3】:非常感谢!
实际上bspeak
已经在单独的 XML 文件中定义,其中标题栏被描述为独立布局。
其他请求很容易可以找到这个视图。我认为在 onCreate 方法中定义 Button 对象是个坏主意。
Button bspeak = (Button) findViewById(R.id.bspeak);
Button bstop = (Button) findViewById(R.id.bstop_mute);
Eclipse 没有改变颜色,也没有像其他按钮那样将单词bspeak
和bstop
斜体化。分别OnClickListener
没有在下面的字符串中设置:
bspeak.setOnClickListener(this);
bstop.setOnClickListener(this);
但即使这样也没有成功。真正的问题是关于字符串
LinearLayout questionLL = (LinearLayout) findViewById(R.id.question_page_titlebar);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.questions_title_bar);
在初始化按钮 bspeak
和 bstop
之后编写。上面这些字符串上升解决了这个问题。有点奇怪。
【讨论】:
以上是关于致命异常:Android 应用程序运行时的主程序的主要内容,如果未能解决你的问题,请参考以下文章
Android - 录制视频后停止和重置相机时的致命信号 11
致命异常:ScioFrontendApi 在使用适用于 Android 应用程序的 Firebase 升级 Google Play 服务时