我正在开发一个应用程序,每次运行它时,我都会收到以下消息:
不幸的是,MyApp 已停止。
我能做什么来解决这个问题?
关于这个问题 - 显然是受到 什么是堆栈跟踪,如何使用它来调试我的应用程序错误?,有很多问题表明他们的应用程序已崩溃,但没有任何进一步的详细信息。这个问题旨在指导新手 Android 程序员如何尝试自己解决问题,或者提出正确的问题。
I am developing an application, and everytime I run it, I get the message:
Unfortunately, MyApp has stopped.
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.
发布评论
评论(23)
首先,您需要检查应用程序崩溃的位置和原因
(不幸的是,MyApp 已停止。)。
在LOG
的帮助下,您可以弄清楚发生了什么错误的。之后,您会发现您的应用程序已停止从您的点修复该点。
First, you need to check where and why your app has been crashed
(Unfortunately, MyApp has stopped.).
With the help ofLOG
, you can figure it out what went wrong.After that, you find which point your app has stopped fix that from your point.
另外,在终端中运行此命令可以帮助找到问题:
然后您应该转到 gradlew 文件位置读取上述两个日志文件。
Also running this command in terminal can help find the problem:
then you should go to gradlew file location in read two above log files.
我会建议这样的东西。
检查您的手机是否具有足够好的空间,该应用程序可以运行-----先验/
在应用程序崩溃时检查LogCat。它将显示应用程序崩溃的确切行。
检查您是否在主线程上使用了由于ANR而使用大量内存的东西。
I'll Suggest something like this.
Check if your phone has good enough space that the app can run----prior/
Check the logcat when the app crashes. It will show the exact line where the app crashed.
Check if you are using something on the main thread that uses a lot of memory due to ANR.
检查您的 logcat 消息。另外,请参阅您的 castest file 对于诸如定义
>活动,
用户许可
等。如果使用 Android Studio ,请参见LogCat,然后按 alt+6 < /em>
或者,
如果您使用 Eclipse ,则
窗口 - &gt;开放透视 - &gt;其他 - logcat
现在,从下拉菜单中选择错误。
另外 ,您可以使用 ADB工具获取
logcat文件
以分析问题。现在,打开
logcat.txt
文件,然后搜索您的应用程序名称。应该有有关为什么失败的信息,行号,班级名称等。Check your Logcat message. Also, see your Manifest file for missing elements like defining the
Activity
,User permission
, etc.To see Logcat if you use Android Studio then Press alt+6
or
if you use Eclipse then
Window -> Open Perspective -> Other - LogCat
Now, from the drop-down menu select error.
Alternatively, you can use ADB tool to get the
Logcat file
to analyze the issue.Now open the
logcat.txt
file and search for your application name. There should be information on why it failed, the line number, Class name, etc.如果您的应用程序崩溃了没有任何错误,而您没有使用资产管理器,而是加载了类似的纹理:
那就是问题所在。我发生了这种情况。您应始终使用资产管理器来避免内存超负荷。
If your app crashed without any errors, and you didn't use an asset manager but loaded the textures like:
then that's the problem. I had that happen to me. You should always use an asset manager to avoid a memory overload.
这个答案描述了检索堆栈跟踪的过程。已经有堆栈跟踪了吗?阅读“什么是堆栈跟踪,如何使用它来调试我的应用程序错误?"
问题
您的应用程序因未捕获的
RuntimeException
而退出被扔了。其中最常见的是
NullPointerException< /代码>
。
怎么解决呢?
每次 Android 应用程序崩溃(或任何 Java 应用程序崩溃)时,堆栈跟踪都会写入控制台(在本例中为 logcat)。该堆栈跟踪包含解决您的问题的重要信息。
Android Studio
在窗口底部栏中,单击
Logcat 按钮。或者,您可以按 alt+6。确保在
设备
面板中选择您的模拟器或设备。接下来,尝试查找堆栈跟踪,它以红色显示。 logcat 中可能记录了很多内容,因此您可能需要滚动一下。找到堆栈跟踪的一个简单方法是清除 logcat(使用右侧的回收站),然后让应用程序再次崩溃。我找到了堆栈跟踪,现在怎么办?
耶!您的问题已经解决了一半。
您只需要通过分析堆栈跟踪来找出到底是什么导致您的应用程序崩溃。
阅读“什么是堆栈跟踪,如何使用它来调试我的应用程序错误?”
我仍然无法解决我的问题!
如果您找到
异常
以及发生该异常的行,但仍然无法弄清楚如何修复它,请随时在 StackOverflow 上提问。尝试尽可能简洁:发布堆栈跟踪和相关代码(例如,直到引发
异常
的行为止的几行)。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.The most common of these is the
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). This stack trace contains vital information for solving your problem.Android Studio
In the bottom bar of the window, click on the
Logcat
button. Alternatively, you can press alt+6. Make sure your emulator or device is selected in theDevices
panel. 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. 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.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.Try to be as concise as possible: post the stack trace, and the relevant code (e.g. a few lines up to the line which threw the
Exception
).您可以使用Google的ADB工具获取
Logcat文件 来分析问题。
打开
logcat.txt
文件并搜索您的应用程序名称。应该有关于失败原因、行号、类名等的信息。You can use Google's ADB tool to get
Logcat file
to analyze the issue.open
logcat.txt
file and search for your application name. There should be information on why it failed, the line number, Class name, etc.首先,您检查了您的应用程序崩溃的要点(
不幸的是,MyApp已停止。
)。为此,您可以使用log.e(“ tag”,“ message”);
,使用此行,您可以在logcat中查看应用程序log。之后,您发现您的应用程序停止了这一点,很容易在您身边解决。
First, you check which point your app has crashed (
Unfortunately, MyApp has stopped.
). For this, you can useLog.e("TAG", "Message");
, using this line you can see your app log in logcat.After that, you find which point your app has stopped it's very easy to solve at your side.
只需检查日志猫中的错误即可。
您可以从Eclipse中获得日志猫选项:
log cat包含错误。
其他明智的方法,您还可以通过在调试模式下执行应用程序来检查错误。
首先通过这样做设置断点:
Just check the error in log cat.
You get the log cat option from in eclipse:
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:
注意:此答案使用的是 Android Studio 2.2.2
注意 2: 我认为您的设备已成功连接。
当应用程序崩溃时,您要做的第一件事就是查看 LogCat,在 Android Studio 的底部有一个带有菜单列表的工具栏:
单击“Android Monitor”(我在^)
现在,您将得到如下内容:
立即将“
Verbose
”更改为“Error
”它只会显示您记录的错误。现在不用担心所有这些错误(如果您遇到了它们)。好的。现在,执行您为使应用程序崩溃所做的操作。应用程序崩溃后,转到 logcat。您应该找到一个新的崩溃日志,其中包含大量
at:xxx
: 和Caused by: TrumpIsPresidentException
例如。转到 logcat 中的Caused by:
语句。在
Caused By:
旁边,应该有发生的异常。就我而言,它是一个RuntimeException
并且在它下面应该有一行包含一个蓝色链接,例如:<强>如果那样的话
Caused by:
下方某处没有带有蓝色文本的行,然后查找另一个具有该内容的Caused by:
。点击蓝色链接。它应该带您到问题发生的地方。就我而言,这是由于这一行:
所以,现在我知道它为什么崩溃了。这是因为我自己抛出异常。 这是一个明显的错误。
但是,假设我遇到了另一个错误:
我检查了 logcat,单击了它给我的蓝色链接,然后它把我带到了这里:
所以,现在我想调试。根据 这个 StackOverflow 问题,一个 NullPointerException表示某些内容为
null
。那么,让我们找出什么是 null。有两种可能性。
mTextView
为 null,或者myString
为 null。为了找出答案,在mTextView.setText(mString)
行之前,我添加了这两行:现在,就像我们之前所做的那样(我们将 Verose 更改为 Error),我们要将“Error”更改为“调试”。因为我们是通过调试来记录的。以下是所有 Log 方法:
因此,由于我们使用了 Log.d,因此我们正在调试中进行检查。这就是为什么我们将其更改为调试。
请注意
Log.d
有第一个参数,在我们的例子中为“AppDebug”。单击 logcat 右上角的“无过滤器”下拉菜单。选择“编辑过滤器配置”,为过滤器命名,然后在“日志标签”中输入“应用程序调试”。单击“确定”。现在,您应该在 logcat 中看到两行:现在我们知道 mTextView 为 null。
我观察我的代码,现在我注意到了一些东西。
我在类的顶部声明了
private TextView mTextView
。但是,我没有定义它。基本上,我忘记在 onCreate() 中执行此操作:
这就是为什么 mTextView 为 null,因为我忘记告诉我的应用程序它是什么。所以我添加了该行,运行我的应用程序,现在应用程序不会崩溃。
Note: This answer is using Android Studio 2.2.2
Note 2: I am considering that your device is successfully connected.
The first thing you do when your application crashes is looking into the LogCat, at the bottom of Android Studio there's a toolbar with a list of menus:
Click on the "Android Monitor" (The one I underlined in the image above. ^)
Now, you'll get something like this:
Change "
Verbose
" to "Error
" Now it will only show you logged errors. Don't worry about all these errors (if you got them) now.Ok. Now, do what you did to crash your app. After your app crashes, go to your logcat. You should find a new crash log that has a lot of
at:x.x.x
: andCaused by: TrumpIsPresidentException
for example. Go to thatCaused by:
statement in your logcat.Next to that
Caused By:
, there should be the Exception that happened. In my case, it's aRuntimeException
and under it there should be a line that contains a blue link such as:If that
Caused by:
DOESN'T have a line with a blue text somewhere under it, then look for anotherCaused by:
that does.Click on that blue link. It should take you to where the problem occurred. In my case, it was due to this line:
So, now I know why it's crashing. It's because I'm throwing the exception myself. This was an obvious error.
However, let's say I got another error:
I checked my logcat, I clicked on the blue link it gave me, and it took me here:
So, now I want to debug. According to this StackOverflow question, a NullPointerException says that something is
null
.So, let's find out what is null. There are two possibilities. Either
mTextView
is null, ormyString
is null. To find out, before themTextView.setText(mString)
line, I add these two lines:Now, like we did previously (We changed Verose to Error), we want to change "Error" to "Debug". Since we're logging by debugging. Here are all the Log methods:
So, since we used
Log.d
, we're checking in Debug. That's why we changed it to debug.Notice
Log.d
has a first parameter,in our case "AppDebug". Click on the "No Filters" drop down menu on the top-right of the logcat. Select "Edit Filter Configuration", give a name to your filter, and in "Log Tag" put "App Debug". Click "OK". Now, you should see two lines in the logcat:So now we know that mTextView is null.
I observe my code, now I notice something.
I have
private TextView mTextView
declared at the top of my class. But, I'm not defining it.Basically, I forgot to do this in my onCreate():
So THAT'S why
mTextView
is null, because I forgot to tell my app what it is. So I add that line, run my app, and now the app doesn't crash.此弹出窗口仅在您在代码中获得致命例外的情况下显示,从而阻止了该应用程序的执行。它可能是任何例外
nullpoInterException
,OutOfMemoryException
等。检查的最佳方法是通过 logcat ,如果您仍在Android Studio中开发App,快速读取堆栈跟踪并检查应用程序原因的方法。
如果您的应用程序已经存在,则无法使用 logcat 。因此,为此,您可以实现
crashlytics
为您提供任何发生任何例外的错误报告。This popup shows only when you get a fatal exception in your code which stops the execution of the app. It could be any exception
NullPointerException
,OutOfMemoryException
etc.Best way to check is through Logcat if you are still developing the app in Android studio which is quick way to read stack trace and check the cause of the app.
If your app is already live, then you can not use logcat. So, for that you can implement
Crashlytics
to provide you bug reports of any exception that occurs.检查您的
logcat
消息,然后查看您的清单
文件。应该缺少某些内容,例如定义活动,
用户权限,等等。Check your
Logcat
message and see yourManifest
file. There should be something missing like defining theActivity,
User permission`, etc.您可以使用这些工具:
建议使用 Android调试显示器,这很好。因为当日志过多时,Eclipse会悬挂,并且通过ADB LogCat过滤器以及所有困难。
You can use any of these tools:
I suggest to use Android Debug Monitor, it is good. Because eclipse hangs when too many logs are there, and through adb logcat filter and all difficult.
您必须检查 IDE 上的
堆栈跟踪
如何执行此操作?
检查 Windows 窗体 LOGCAT
如果您看不到 logcat 窗口,请转到此路径并打开它
(如果您这样 做)正在使用 Google-Api 转到此路径
adb logcat > logcat.txt
You have to check the
Stack trace
How to do that?
on Your IDE Check the windows form LOGCAT
If you cant see the logcat windows go to this path and open it
if you are using Google-Api go to this path
adb logcat > logcat.txt
在下面的showtoast()方法中,您必须通过这样做将另一个参数传递给上下文或应用程序上下文。
In below showToast() method you have to pass another parameter for context or application context by doing so you can try it.
让我分享一下当您遇到强制关闭(当应用程序停止工作时)时的基本 Logcat 分析。
DOCS
Android 中收集/分析日志的基本工具是 logcat。
这里是关于 logcat 的 Android 页面
如果您使用 android Studio,您还可以检查此LINK。
捕获
基本上,您可以使用以下命令手动捕获 logcat(或者仅检查 AndroidStudio 中的 AndroidMonitor 窗口):
您可以在命令中添加很多参数,以帮助您过滤和显示以下消息:你想要...这是个人的...我总是使用下面的命令来获取消息时间戳:
您可以将输出重定向到文件并在文本编辑器中分析它。
分析
如果您的应用崩溃,您会得到类似以下内容:
日志的这一部分向您显示大量信息:
07-09 08:29:13.475< /code>
检查问题何时发生非常重要...您可能会在日志中发现多个错误...您必须确保检查正确的消息:)
com.example.khan.abc
这样,您就知道哪个应用程序崩溃了(以确保您正在检查有关消息的日志)
java.lang.NullPointerException
空指针异常错误
尝试在空对象引用上调用虚拟方法 'void android.support.v4.app.FragmentActivity.onBackPressed()'
您尝试从
FragmentActivity
对象调用方法onBackPressed()
。但是,当您执行此操作时,该对象为null
。堆栈跟踪:堆栈跟踪向您显示方法调用顺序...有时,错误发生在调用方法中(而不是发生在被调用方法中)。
at com.example.khan.abc.AudioFragment$1.onClick(AudioFragment.java:125)
文件
com.example.khan.abc.AudioFragment.java
内onClick() 发生错误
方法位于行:125
(堆栈跟踪显示发生错误的行)它被调用者:
被调用者:
被调用者:
等等...
概述
这只是一个概述...并非所有日志都很简单,但错误给出了具体问题,详细显示了所有问题...这只是分享想法并提供给你的入门级信息...
我希望我能以某种方式帮助你...
问候
Let me share a basic Logcat analysis for when you meet a Force Close (when the app stops working).
DOCS
The basic tool from Android to collect/analyze logs is the logcat.
HERE is the Android's page about logcat
If you use android Studio, you can also check this LINK.
Capturing
Basically, you can MANUALLY capture logcat with the following command (or just check AndroidMonitor window in AndroidStudio):
There's a lot of parameters you can add to the command which helps you to filter and display the message that you want... This is personal... I always use the command below to get the message timestamp:
You can redirect the output to a file and analyze it in a Text Editor.
Analyzing
If you app is Crashing, you'll get something like:
This part of the log shows you a lot of information:
07-09 08:29:13.475
It is important to check when the issue happened... You may find several errors in a log... you must be sure that you are checking the proper messages :)
com.example.khan.abc
This way, you know which app crashed (to be sure that you are checking the logs about your message)
java.lang.NullPointerException
A NULL Pointer Exception error
Attempt to invoke virtual method 'void android.support.v4.app.FragmentActivity.onBackPressed()' on a null object reference
You tried to call method
onBackPressed()
from aFragmentActivity
object. However, that object wasnull
when you did it.Stack Trace: Stack Trace shows you the method invocation order... Sometimes, the error happens in the calling method (and not in the called method).
at com.example.khan.abc.AudioFragment$1.onClick(AudioFragment.java:125)
Error happened in file
com.example.khan.abc.AudioFragment.java
, insideonClick()
method at line:125
(stacktrace shows the line that error happened)It was called by:
Which was called by:
which was called by:
etc....
Overview
This was just an overview... Not all logs are simple but the error gives specific problem and verbose shows up all problem ... It is just to share the idea and provide entry-level information to you...
I hope I could help you someway...
Regards
使用 LogCat 并尝试查找导致应用程序崩溃的原因。
要查看 Logcat(如果您使用 Android Studio),请按 ALT + 6
或者
如果您使用 Eclipse 那么
窗口->开放视角->其他 - LogCat
转到 LogCat,从下拉菜单中选择错误。这将包含帮助您调试所需的所有信息。如果这没有帮助,请将 LogCat 作为对您的问题的编辑发布,有人会帮助您。
Use the LogCat and try to find what is causing the app to crash.
To see Logcat if you use Android Studio then Press ALT + 6
or
if you use Eclipse then
Window -> Open Perspective -> Other - LogCat
Go to the LogCat, from the drop down menu select error. This will contain all the required information to help you debug. If that doesn't help, post the LogCat as an edit to your question and somebody will help you out.
如果您的应用程序出于某种原因而没有良好的StackTrace崩溃。尝试从第一行进行调试,然后按行行直至崩溃。然后,您将得到答案,哪一行会给您带来麻烦。然后,您可以将其包装到尝试捕获块并打印错误输出中。
If your app for some reason crashes without good stacktrace. Try debug it from first line, and go line by line until crash. Then you will have answer, which line is causing you trouble. Proably you could then wrapp it into try catch block and print error output.
您还可以自己收到此错误消息,而无需任何堆栈跟踪或任何其他错误消息。
在这种情况下,您需要确保正确配置了Android清单(包括从库中发生的任何明显合并以及将来自库的任何活动),并特别注意您的清单文件中应用程序中显示的第一个活动。
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.
人们会犯错误,编码也会犯错误。
发生任何
错误
时,请始终使用红色文本的 logcat 进行检查,但是您可以在红色文本中找到带下划线的蓝色文本中的真正问题 。确保如果您创建新的
activity
,请始终在AndroidManifest
文件中声明该activity
。如果添加权限,也要在
AndroidMainifest
文件中声明它。People make mistakes, and so coding as well.
When ever any
error
happened, always check with the logcat with the text in red color however u can find out the real problem in blue color text with underline in those red color text.Make sure if u create a new
activity
, always declare theactivity
in theAndroidManifest
file.If adding Permission, declare it in the
AndroidMainifest
file as well.logcat - 检查Android Studio的开发阶段的日志
最初清除了LogCat,然后再次让应用程序崩溃,以便您只能获得崩溃的日志详细信息。您必须检查堆栈跟踪
应用程序崩溃期间的常见错误如下:
解决应用程序崩溃错误:
Logcat - To check the logs in the development phase of Android Studio
Initially clear the Logcat and let the app crash again so you can get only crashed log detail. You have to check the Stack trace
Common error during app crash like:
To resolve app crash error:
开发期间崩溃
尝试我最喜欢的工具 logview 来获取日志并分析它们在开发过程中。
在 Linux 中运行时,请确保将
./logview
和./lib/logview.jar
标记为可执行文件。如果您不喜欢它,还有很多替代的Android 桌面日志查看器。
野外崩溃
集成实时崩溃报告工具,例如 Firebase Crashlytics,以便获取用户设备上发生的未处理异常的堆栈跟踪。
阅读如何发布一个有问题的应用程序(并现场讲述故事)以了解有关现场处理错误的更多信息。
Crash during development
Try my favourite tool logview to get the logs and analyze them during development.
Make sure to mark
./logview
and./lib/logview.jar
as executable when running in Linux.If you don't like it, there're a lot of alternative desktop log viewers for Android.
Crash in the wild
Integrate a real-time crash reporting tool such as Firebase Crashlytics in order to get stacktraces of unhandled exceptions which occurred on users' devices.
Read How to Release a Buggy App (And Live to Tell the Tale) to know more about handling bugs in the field.
如果您的终端中没有任何有趣的日志(或者它们与您的应用程序不直接相关),则您的问题可能是由于本机库造成的。在这种情况下,您应该检查终端中的“tombstone”文件。
逻辑删除文件的默认位置取决于每个设备,但如果是这种情况,您将收到一条日志,告诉您:
逻辑删除文件已写入:/data/tombstones/tombstone_06
有关详细信息,请查看https://source.android.com/devices/tech/debug。
If you don't have any kind of interesting log in your terminal (or they are not directly related to your app), maybe your problem is due to a native library. In that case, you should check for the "tombstone" files within your terminal.
The default location for the tombstone files depends on every device, but if that's the case, you will have a log telling:
Tombstone written to: /data/tombstones/tombstone_06
For more information, check on https://source.android.com/devices/tech/debug.