不幸的是MyApp已停止。 我该如何解决?

本文翻译自:Unfortunately MyApp has stopped. How can I solve this?

I am developing an application, and everytime I run it, I get the message: 我正在开发一个应用程序,每次运行它时,都会收到以下消息:

Unfortunately, MyApp has stopped. 不幸的是,MyApp已停止。

What can I do to solve this? 我该怎么解决?


About this question - obviously inspired by What is a stack trace, and how can I use it to debug my application errors? 关于这个问题-显然是受什么是堆栈跟踪的启发,以及如何使用它来调试应用程序错误? , there are lots of questions stating that their application has crashed, without any further detail. ,有很多问题表明它们的应用已崩溃,没有任何进一步的细节。 This question aims to instruct novice Android programmers on how to try and fix their problems themselves, or ask the right questions. 这个问题的目的是指导新手Android程序员如何自行解决问题或提出正确的问题。


#1楼

参考:https://stackoom.com/question/1ZzE5/不幸的是MyApp已停止-我该如何解决


#2楼

This answer describes the process of retrieving the stack trace. 此答案描述了检索堆栈跟踪的过程。 Already have the stack trace? 已经有堆栈跟踪了吗? Read up on stack traces in " What is a stack trace, and how can I use it to debug my application errors? " 在“ 什么是堆栈跟踪,以及如何使用它来调试应用程序错误? ”中阅读堆栈跟踪。

The Problem 问题

Your application quit because an uncaught RuntimeException was thrown. 您的应用程序退出,因为引发了未捕获的RuntimeException
The most common of these is the NullPointerException . 其中最常见的是NullPointerException

How to solve it? 怎么解决呢?

Every time an Android application crashes (or any Java application for that matter), a Stack trace is written to the console (in this case, logcat). 每当Android应用程序崩溃(或与此相关的任何Java应用程序)崩溃时,都会将Stack trace写入控制台(在本例中为logcat)。 This stack trace contains vital information for solving your problem. 此堆栈跟踪包含用于解决问题的重要信息。

Android Studio Android Studio

在Android Studio中查找堆栈跟踪

In the bottom bar of the window, click on the Logcat button. 在窗口的底部栏中,单击Logcat按钮。 Alternatively, you can press alt + 6 . 或者,您可以按alt + 6 Make sure your emulator or device is selected in the Devices panel. 确保在“ Devices面板中选择了仿真器或设备。 Next, try to find the stack trace, which is shown in red. 接下来,尝试找到以红色显示的堆栈跟踪。 There may be a lot of stuff logged into logcat, so you may need to scroll a bit. 可能有很多东西登录到logcat中,因此您可能需要滚动一下。 An easy way to find the stack trace is to clear the logcat (using the recycle bin on the right), and let the app crash again. 查找堆栈跟踪的一种简单方法是清除logcat(使用右侧的回收站),然后让应用再次崩溃。

I have found the stack trace, now what? 我已经找到了堆栈跟踪,现在呢?

Yay! 好极了! You're halfway to solving your problem. 您已经解决了一半问题。
You only need to find out what exactly made your application crash, by analyzing the stack trace. 您只需分析堆栈跟踪,找出导致应用程序崩溃的原因。

Read up on stack traces in " What is a stack trace, and how can I use it to debug my application errors? " 在“ 什么是堆栈跟踪,以及如何使用它来调试应用程序错误? ”中阅读堆栈跟踪。

I still can't solve my problem! 我仍然无法解决我的问题!

If you've found your Exception and the line where it occurred, and still cannot figure out how to fix it, don't hesitate to ask a question on StackOverflow. 如果您找到了Exception及其发生的行,但仍然想不出解决方法,请随时在StackOverflow上提问。

Try to be as concise as possible: post the stack trace, and the relevant code (eg a few lines up to the line which threw the Exception ). 尽量简明扼要:发布堆栈跟踪和相关代码(例如,在抛出Exception行之前增加几行)。


#3楼

First, you check which point your app has crashed ( Unfortunately, MyApp has stopped. ). 首先,检查您的应用崩溃了( Unfortunately, MyApp has stopped. )。 For this, you can use Log.e("TAG", "Message"); 为此,可以使用Log.e("TAG", "Message"); , using this line you can see your app log in logcat. ,使用此行,您可以在logcat中看到您的应用程序日志。

After that, you find which point your app has stopped it's very easy to solve at your side. 之后,您会发现您的应用停止了哪一点,这很容易在您身边解决。


#4楼

You can also get this error message on its own, without any stack trace or any further error message. 您也可以单独获取此错误消息,而无需任何堆栈跟踪或任何其他错误消息。

In this case you need to make sure your Android manifest is configured correctly (including any manifest merging happening from a library and any activity that would come from a library), and pay particular attention to the first activity displayed in your application in your manifest files. 在这种情况下,您需要确保正确配置了Android清单(包括库中发生的所有清单合并以及库中发生的任何活动),并特别注意清单文件中应用程序中显示的第一个活动。


#5楼

You can use Google's ADB tool to get Logcat file to analyze the issue. 您可以使用Google的ADB工具获取Logcat file来分析问题。

adb logcat > logcat.txt

open logcat.txt file and search for your application name. 打开logcat.txt文件并搜索您的应用程序名称。 There should be information on why it failed, the line number, Class name, etc. 应该有关于失败原因,行号,类名等的信息。


#6楼

Just check the error in log cat. 只需检查日志猫中的错误。

You get the log cat option from in eclipse: 您可以从Eclipse中获取log cat选项:

window->show view->others->Android->Logcat 窗口->显示视图->其他-> Android-> Logcat

Log cat contains error. 日志猫包含错误。

Other wise you can also check the error by executing an application in debug mode. 否则,您还可以通过在调试模式下执行应用程序来检查错误。 Firstly set breakpoint after that by doing: 首先通过以下操作设置断点:

right click on project->debug as->Android application 右键单击项目->调试为-> Android应用程序

发布了0 篇原创文章 · 获赞 8 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/105434032