我怎么知道是啥引发了异常?

Posted

技术标签:

【中文标题】我怎么知道是啥引发了异常?【英文标题】:How can I know what threw the exception?我怎么知道是什么引发了异常? 【发布时间】:2019-07-06 05:32:03 【问题描述】:

首先我要说的是,我通常知道如何找到我的异常。但是我遇到了一种情况,stackstrace 不显示我的任何类,而只显示库类。

我试过调试代码没有成功。

这是堆栈跟踪

E/androidRuntime: FATAL EXCEPTION: main
Process: com.app.ys571.realdeal, PID: 11175
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
    at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:396)
    at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:391)
    at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:107)
    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1010)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1224)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1092)
    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1622)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
    at android.view.View.measure(View.java:23169)
    at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1119)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at com.android.internal.policy.DecorView.onMeasure(DecorView.java:716)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2718)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1572)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1855)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
    at android.view.Choreographer.doCallbacks(Choreographer.java:761)
    at android.view.Choreographer.doFrame(Choreographer.java:696)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

当我尝试使用我的导航抽屉从我的主要活动移动到另一个活动时,它是堆栈跟踪。 它可能在新活动 onResume 之后终止。 我不能确切地知道从什么地方到哪里。

谢谢!

【问题讨论】:

【参考方案1】:

看起来这与ViewPager 有关。我假设在你的新活动中你有一个ViewPager,我猜它没有被正确实例化。查看this question,它似乎有同样的问题并提供了一些解决方案。

【讨论】:

【参考方案2】:

如果您使用 Android Studio,那么您可以在调试时使用异常断点。这是a link,您可以查看示例。

【讨论】:

它仍然没有显示它在我的代码中发生的位置。我只能假设...

以上是关于我怎么知道是啥引发了异常?的主要内容,如果未能解决你的问题,请参考以下文章

测试装饰器引发的异常的好方法是啥?

python:我怎么知道发生了啥类型的异常?

引发类型为“System.OutOfMemoryException”的异常.是啥错误

在 C# 中重新引发异常的正确方法是啥? [复制]

启动visual studio时taskscheduler引发了异常是啥原因?

在任务中捕获异常的最佳方法是啥?