如果我在我的应用程序中抛出异常,它将按预期崩溃,显示消息“不幸的是已停止”。然而,它显示此消息然后打开另一个活动并再次显示该消息似乎很奇怪。它基本上遍历我的所有活动,在应用重启之前显示此消息。
我通过将TextView
转换为LinearLayout
来抛出测试异常,这是输出:
01-28 12:04:36.270: E/AndroidRuntime(23772): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.LinearLayout
01-28 12:04:36.270: E/AndroidRuntime(23772): at com.iii.News.onCreate(News.java:16)
01-28 12:04:36.270: E/AndroidRuntime(23772): at android.app.Activity.performCreate(Activity.java:5104)
01-28 12:04:36.270: E/AndroidRuntime(23772): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-28 12:04:36.270: E/AndroidRuntime(23772): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-28 12:04:36.270: E/AndroidRuntime(23772): ... 11 more
01-28 12:04:42.706: D/dalvikvm(23873): newInstance failed: no <init>()
01-28 12:04:42.706: D/AndroidRuntime(23873): Shutting down VM
01-28 12:04:42.706: W/dalvikvm(23873): threadid=1: thread exiting with uncaught exception (group=0x41ff3930)
01-28 12:04:42.706: E/AndroidRuntime(23873): FATAL EXCEPTION: main
01-28 12:04:42.706: E/AndroidRuntime(23873): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iii/com.iii.Forums}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.iii.menu.TradingMenuFragment: make sure class name exists, is public, and has an empty constructor that is public
01-28 12:04:42.706: E/AndroidRuntime(23873): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-28 12:04:42.706: E/AndroidRuntime(23873): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-28 12:04:42.706: E/AndroidRuntime(23873): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-28 12:04:42.706: E/AndroidRuntime(23873): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-28 12:04:42.706: E/AndroidRuntime(23873): at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 12:04:42.706: E/AndroidRuntime(23873): at android.os.Looper.loop(Looper.java:137)
01-28 12:04:42.706: E/AndroidRuntime(23873): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-28 12:04:42.706: E/AndroidRuntime(23873): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 12:04:42.706: E/AndroidRuntime(23873): at java.lang.reflect.Method.invoke(Method.java:511)
01-28 12:04:42.706: E/AndroidRuntime(23873): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-28 12:04:42.706: E/AndroidRuntime(23873): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-28 12:04:42.706: E/AndroidRuntime(23873): at dalvik.system.NativeStart.main(Native Method)
01-28 12:04:42.706: E/AndroidRuntime(23873): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.iii.menu.TradingMenuFragment: make sure class name exists, is public, and has an empty constructor that is public
01-28 12:04:42.706: E/AndroidRuntime(23873): at android.support.v4.app.Fragment.instantiate(Fragment.java:399)
01-28 12:04:42.706: E/AndroidRuntime(23873): at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
01-28 12:04:42.706: E/AndroidRuntime(23873): at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1760)
01-28 12:04:42.706: E/AndroidRuntime(23873): at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:200)
01-28 12:04:42.706: E/AndroidRuntime(23873): at com.slidingmenu.lib.app.SlidingFragmentActivity.onCreate(SlidingFragmentActivity.java:20)
01-28 12:04:42.706: E/AndroidRuntime(23873): at com.iii.BaseActivity.onCreate(BaseActivity.java:29)
01-28 12:04:42.706: E/AndroidRuntime(23873): at com.iii.Forums.onCreate(Forums.java:9)
01-28 12:04:42.706: E/AndroidRuntime(23873): at android.app.Activity.performCreate(Activity.java:5104)
01-28 12:04:42.706: E/AndroidRuntime(23873): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-28 12:04:42.706: E/AndroidRuntime(23873): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-28 12:04:42.706: E/AndroidRuntime(23873): ... 11 more
01-28 12:04:42.706: E/AndroidRuntime(23873): Caused by: java.lang.InstantiationException: can't instantiate class com.iii.menu.TradingMenuFragment; no empty constructor
01-28 12:04:42.706: E/AndroidRuntime(23873): at java.lang.Class.newInstanceImpl(Native Method)
01-28 12:04:42.706: E/AndroidRuntime(23873): at java.lang.Class.newInstance(Class.java:1319)
01-28 12:04:42.706: E/AndroidRuntime(23873): at android.support.v4.app.Fragment.instantiate(Fragment.java:388)
01-28 12:04:42.706: E/AndroidRuntime(23873): ... 20 more
01-28 12:04:57.442: D/dalvikvm(23901): newInstance failed: no <init>()
01-28 12:04:57.442: D/AndroidRuntime(23901): Shutting down VM
01-28 12:04:57.442: W/dalvikvm(23901): threadid=1: thread exiting with uncaught exception (group=0x41ff3930)
01-28 12:04:57.442: E/AndroidRuntime(23901): FATAL EXCEPTION: main
01-28 12:04:57.442: E/AndroidRuntime(23901): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iii/com.iii.Markets}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.iii.menu.TradingMenuFragment: make sure class name exists, is public, and has an empty constructor that is public
01-28 12:04:57.442: E/AndroidRuntime(23901): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-28 12:04:57.442: E/AndroidRuntime(23901): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-28 12:04:57.442: E/AndroidRuntime(23901): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-28 12:04:57.442: E/AndroidRuntime(23901): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-28 12:04:57.442: E/AndroidRuntime(23901): at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 12:04:57.442: E/AndroidRuntime(23901): at android.os.Looper.loop(Looper.java:137)
01-28 12:04:57.442: E/AndroidRuntime(23901): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-28 12:04:57.442: E/AndroidRuntime(23901): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 12:04:57.442: E/AndroidRuntime(23901): at java.lang.reflect.Method.invoke(Method.java:511)
01-28 12:04:57.442: E/AndroidRuntime(23901): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-28 12:04:57.442: E/AndroidRuntime(23901): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-28 12:04:57.442: E/AndroidRuntime(23901): at dalvik.system.NativeStart.main(Native Method)
01-28 12:04:57.442: E/AndroidRuntime(23901): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.iii.menu.TradingMenuFragment: make sure class name exists, is public, and has an empty constructor that is public
01-28 12:04:57.442: E/AndroidRuntime(23901): at android.support.v4.app.Fragment.instantiate(Fragment.java:399)
01-28 12:04:57.442: E/AndroidRuntime(23901): at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
01-28 12:04:57.442: E/AndroidRuntime(23901): at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1760)
01-28 12:04:57.442: E/AndroidRuntime(23901): at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:200)
01-28 12:04:57.442: E/AndroidRuntime(23901): at com.slidingmenu.lib.app.SlidingFragmentActivity.onCreate(SlidingFragmentActivity.java:20)
01-28 12:04:57.442: E/AndroidRuntime(23901): at com.iii.BaseActivity.onCreate(BaseActivity.java:29)
01-28 12:04:57.442: E/AndroidRuntime(23901): at com.iii.Markets.onCreate(Markets.java:9)
01-28 12:04:57.442: E/AndroidRuntime(23901): at android.app.Activity.performCreate(Activity.java:5104)
01-28 12:04:57.442: E/AndroidRuntime(23901): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-28 12:04:57.442: E/AndroidRuntime(23901): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-28 12:04:57.442: E/AndroidRuntime(23901): ... 11 more
01-28 12:04:57.442: E/AndroidRuntime(23901): Caused by: java.lang.InstantiationException: can't instantiate class com.iii.menu.TradingMenuFragment; no empty constructor
01-28 12:04:57.442: E/AndroidRuntime(23901): at java.lang.Class.newInstanceImpl(Native Method)
01-28 12:04:57.442: E/AndroidRuntime(23901): at java.lang.Class.newInstance(Class.java:1319)
01-28 12:04:57.442: E/AndroidRuntime(23901): at android.support.v4.app.Fragment.instantiate(Fragment.java:388)
01-28 12:04:57.442: E/AndroidRuntime(23901): ... 20 more
答案 0 :(得分:2)
所以,显然你正在尝试使用TradingMenuFragment
,但正如你从logcat中看到的那样,它没有空的构造函数。
01-28 12:04:42.706: E/AndroidRuntime(23873): Caused by: java.lang.InstantiationException:
can't instantiate class com.iii.menu.TradingMenuFragment; no empty constructor
解决这个问题,一切都应该没问题。