Java 不幸的是,MyApp 已停止。我该如何解决这个问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23353173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unfortunately MyApp has stopped. How can I solve this?
提问by nhaarman
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 程序员如何尝试自己解决问题,或提出正确的问题。
采纳答案by nhaarman
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
.
您的应用程序退出是因为RuntimeException
抛出了一个未捕获的事件。
其中最常见的是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 应用程序(或任何与此相关的 Java 应用程序)崩溃时,都会将 aStack trace
写入控制台(在本例中为 logcat)。此堆栈跟踪包含解决问题的重要信息。
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 the Devices
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.
在窗口的底部栏中,单击Logcat
按钮。或者,您可以按alt+ 6。确保在Devices
面板中选择了您的模拟器或设备。接下来,尝试找到以红色显示的堆栈跟踪。可能有很多东西登录到 logcat,所以你可能需要滚动一下。找到堆栈跟踪的一个简单方法是清除 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 relevantcode (e.g. a few lines up to the line which threw the Exception
).
尽量简洁:发布堆栈跟踪和相关代码(例如,在抛出 的行之前的几行Exception
)。
回答by Hiren Vaghela
First, you check which point your app has crashed (Unfortunately, MyApp has stopped.
). For this, you can use Log.e("TAG", "Message");
, using this line you can see your app log in logcat.
首先,您检查应用程序崩溃的点 ( Unfortunately, MyApp has stopped.
)。为此,您可以使用Log.e("TAG", "Message");
,使用此行您可以在 logcat 中看到您的应用程序登录。
After that, you find which point your app has stopped it's very easy to solve at your side.
之后,您会发现您的应用程序停止了哪一点,这很容易在您身边解决。
回答by Pelpotronic
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 清单配置正确(包括来自库的任何清单合并以及来自库的任何活动),并特别注意清单文件中应用程序中显示的第一个活动.
回答by Vlad Bezden
You can use Google's ADB toolto get Logcat file
to analyze the issue.
您可以使用谷歌的ADB工具获得Logcat file
分析问题。
adb logcat > logcat.txt
open logcat.txt
file and search for your application name. There should be information on why it failed, the line number, Class name, etc.
打开logcat.txt
文件并搜索您的应用程序名称。应该有关于失败原因、行号、类名等的信息。
回答by Rahil Ali
Just check the error in log cat.
只需检查 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.
日志 cat 包含错误。
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 应用程序
回答by Manoj ahirwar
Check your Logcat
message and see your Manifest
file. There should be something missing like defining the Activity,
User permission`, etc.
检查您的Logcat
消息并查看您的Manifest
文件。应该缺少一些东西,比如定义Activity,
用户权限等。
回答by alireza amini
You have to check the Stack trace
你必须检查 Stack trace
How to do that?
怎么做?
on Your IDE Check the windows form LOGCAT
在您的 IDE 上检查 Windows 窗体 LOGCAT
If you cant see the logcat windows go to this path and open it
如果您看不到 logcat 窗口,请转到此路径并打开它
window->show view->others->Android->Logcat
if you are using Google-Api go to this path
如果您使用的是 Google-Api,请转到此路径
adb logcat > logcat.txt
adb logcat > logcat.txt
回答by Shiv Buyya
You can use any of these tools:
您可以使用以下任何工具:
adb logcat
adb logcat > logs.txt (you can use editors to open and search errors.)
eclipse logcat (If not visible in eclipse, Go to Windows->Show View->Others->Android->LogCat)
- Android Debug Monitor or Android Device Monitor(type command monitoror open through UI)
亚行日志猫
adb logcat > logs.txt(您可以使用编辑器打开和搜索错误。)
eclipse logcat(如果在eclipse中不可见,转到Windows->Show View->Others->Android->LogCat)
- Android Debug Monitor 或 Android Device Monitor(键入命令监视器或通过 UI 打开)
- Android Studio
- 安卓工作室
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.
我建议使用Android Debug Monitor,它很好。因为当有太多日志时 eclipse 挂起,并且通过 adb logcat 过滤器和所有困难。
回答by Biswajit Karmakar
Use the LogCatand try to find what is causing the app to crash.
使用LogCat并尝试找出导致应用程序崩溃的原因。
To see Logcat if you use Android Studiothen Press ALT + 6or
如果您使用Android Studio,要查看 Logcat,然后按ALT + 6或
if you use Eclipsethen Window -> Open Perspective -> Other - LogCat
如果您使用Eclipse,则 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.
转到 LogCat,从下拉菜单中选择错误。这将包含帮助您调试的所有必需信息。如果这没有帮助,请将 LogCat 作为对您问题的编辑发布,有人会帮助您。
回答by W0rmH0le
Let me share a basic Logcat analysis for when you meet a Force Close (when the app stops working).
当您遇到强制关闭(当应用程序停止工作时)时,让我分享一个基本的 Logcat 分析。
DOCS
文件
The basic tool from Android to collect/analyze logs is the logcat.
Android 收集/分析日志的基本工具是 logcat。
HEREis the Android's page about logcat
这里是关于 logcat 的 Android 页面
If you use android Studio, you can also check this LINK.
如果您使用 android Studio,您还可以查看此LINK。
Capturing
捕捉
Basically, you can MANUALLY capture logcat with the following command (or just check AndroidMonitor window in AndroidStudio):
基本上,您可以使用以下命令手动捕获 logcat(或仅检查 AndroidStudio 中的 AndroidMonitor 窗口):
adb logcat
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:
有很多参数可以添加到命令中,这些参数可以帮助您过滤和显示您想要的消息……这是个人的……我总是使用下面的命令来获取消息时间戳:
adb logcat -v time
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:
如果您的应用程序崩溃,您会得到如下信息:
07-09 08:29:13.474 21144-21144/com.example.khan.abc D/AndroidRuntime: Shutting down VM
07-09 08:29:13.475 21144-21144/com.example.khan.abc E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.khan.abc, PID: 21144
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.FragmentActivity.onBackPressed()' on a null object reference
at com.example.khan.abc.AudioFragment.onClick(AudioFragment.java:125)
at android.view.View.performClick(View.java:4848)
at android.view.View$PerformClick.run(View.java:20262)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5631)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
07-09 08:29:15.195 21144-21144/com.example.khan.abc I/Process: Sending signal. PID: 21144 SIG: 9
This part of the log shows you a lot of information:
日志的这一部分向您展示了很多信息:
- When the issue happened:
07-09 08:29:13.475
- 问题发生时:
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 :)
检查问题发生的时间很重要...您可能会在日志中发现几个错误...您必须确保您正在检查正确的消息:)
- Which app crashed:
com.example.khan.abc
- 哪个应用程序崩溃了:
com.example.khan.abc
This way, you know which app crashed (to be sure that you are checking the logs about your message)
这样,您就知道哪个应用程序崩溃了(确保您正在检查有关您的消息的日志)
- Which ERROR:
java.lang.NullPointerException
- 哪个错误:
java.lang.NullPointerException
A NULL Pointer Exception error
空指针异常错误
- Detailed info about the error:
Attempt to invoke virtual method 'void android.support.v4.app.FragmentActivity.onBackPressed()' on a null object reference
- 有关错误的详细信息:
Attempt to invoke virtual method 'void android.support.v4.app.FragmentActivity.onBackPressed()' on a null object reference
You tried to call method onBackPressed()
from a FragmentActivity
object. However, that object was null
when you did it.
您试图onBackPressed()
从FragmentActivity
对象调用方法。然而,那个对象是null
你做的时候。
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)
堆栈跟踪:堆栈跟踪显示方法调用顺序......有时,错误发生在调用方法中(而不是在被调用方法中)。
在 com.example.khan.abc.AudioFragment$1.onClick(AudioFragment.java:125)
Error happened in file com.example.khan.abc.AudioFragment.java
, inside onClick()
method at line: 125
(stacktrace shows the line that error happened)
错误发生在文件中com.example.khan.abc.AudioFragment.java
,onClick()
方法内部的行:(125
堆栈跟踪显示发生错误的行)
It was called by:
它被称为:
at android.view.View.performClick(View.java:4848)
Which was called by:
被称为:
at android.view.View$PerformClick.run(View.java:20262)
which was called by:
由以下人员调用:
at android.os.Handler.handleCallback(Handler.java:815)
etc....
等等....
Overview
概述
This was just an overview... Not all logs are simple etc... It is just to share the idea and provide entry-level information to you...
这只是一个概述......并非所有日志都很简单等等......只是为了分享想法并向您提供入门级信息......
I hope I could help you someway... Regards
我希望我能以某种方式帮助你......问候