当前位置:   article > 正文

Android studio 调试时,出现错误闪退,但是控制台没有打印错误信息_ieae调试安卓控制台不输出信息

ieae调试安卓控制台不输出信息

解决途径:

可以监视一下 RuntimeInit.UncaughtHandler 这个类的uncaughtException这个方法


贴上代码给予参考

  1. /*
  2. * Copyright (C) 2006 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.android.internal.os;
  17. import android.app.ActivityManagerNative;
  18. import android.app.ActivityThread;
  19. import android.app.ApplicationErrorReport;
  20. import android.os.Build;
  21. import android.os.Debug;
  22. import android.os.IBinder;
  23. import android.os.Process;
  24. import android.os.SystemProperties;
  25. import android.util.Log;
  26. import android.util.Slog;
  27. import com.android.internal.logging.AndroidConfig;
  28. import com.android.server.NetworkManagementSocketTagger;
  29. import dalvik.system.VMRuntime;
  30. import java.lang.reflect.Method;
  31. import java.lang.reflect.Modifier;
  32. import java.util.TimeZone;
  33. import java.util.logging.LogManager;
  34. import org.apache.harmony.luni.internal.util.TimezoneGetter;
  35. /**
  36. * Main entry point for runtime initialization. Not for
  37. * public consumption.
  38. * @hide
  39. */
  40. public class RuntimeInit {
  41. private final static String TAG = "AndroidRuntime";
  42. private final static boolean DEBUG = false;
  43. /** true if commonInit() has been called */
  44. private static boolean initialized;
  45. private static IBinder mApplicationObject;
  46. private static volatile boolean mCrashing = false;
  47. private static final native void nativeZygoteInit();
  48. private static final native void nativeFinishInit();
  49. private static final native void nativeSetExitWithoutCleanup(boolean exitWithoutCleanup);
  50. private static int Clog_e(String tag, String msg, Throwable tr) {
  51. return Log.println_native(Log.LOG_ID_CRASH, Log.ERROR, tag,
  52. msg + '\n' + Log.getStackTraceString(tr));
  53. }
  54. /**
  55. * Use this to log a message when a thread exits due to an uncaught
  56. * exception. The framework catches these for the main threads, so
  57. * this should only matter for threads created by applications.
  58. */
  59. private static class UncaughtHandler implements Thread.UncaughtExceptionHandler {
  60. public void uncaughtException(Thread t, Throwable e) {
  61. try {
  62. // Don't re-enter -- avoid infinite loops if crash-reporting crashes.
  63. if (mCrashing) return;
  64. mCrashing = true;
  65. if (mApplicationObject == null) {
  66. Clog_e(TAG, "*** FATAL EXCEPTION IN SYSTEM PROCESS: " + t.getName(), e);
  67. } else {
  68. StringBuilder message = new StringBuilder();
  69. message.append("FATAL EXCEPTION: ").append(t.getName()).append("\n");
  70. final String processName = ActivityThread.currentProcessName();
  71. if (processName != null) {
  72. message.append("Process: ").append(processName).append(", ");
  73. }
  74. message.append("PID: ").append(Process.myPid());
  75. Clog_e(TAG, message.toString(), e);
  76. }
  77. // Bring up crash dialog, wait for it to be dismissed
  78. ActivityManagerNative.getDefault().handleApplicationCrash(
  79. mApplicationObject, new ApplicationErrorReport.CrashInfo(e));
  80. } catch (Throwable t2) {
  81. try {
  82. Clog_e(TAG, "Error reporting crash", t2);
  83. } catch (Throwable t3) {
  84. // Even Clog_e() fails! Oh well.
  85. }
  86. } finally {
  87. // Try everything to make sure this process goes away.
  88. Process.killProcess(Process.myPid());
  89. System.exit(10);
  90. }
  91. }
  92. }
  93. private static final void commonInit() {
  94. if (DEBUG) Slog.d(TAG, "Entered RuntimeInit!");
  95. /* set default handler; this applies to all threads in the VM */
  96. Thread.setDefaultUncaughtExceptionHandler(new UncaughtHandler());
  97. /*
  98. * Install a TimezoneGetter subclass for ZoneInfo.db
  99. */
  100. TimezoneGetter.setInstance(new TimezoneGetter() {
  101. @Override
  102. public String getId() {
  103. return SystemProperties.get("persist.sys.timezone");
  104. }
  105. });
  106. TimeZone.setDefault(null);
  107. /*
  108. * Sets handler for java.util.logging to use Android log facilities.
  109. * The odd "new instance-and-then-throw-away" is a mirror of how
  110. * the "java.util.logging.config.class" system property works. We
  111. * can't use the system property here since the logger has almost
  112. * certainly already been initialized.
  113. */
  114. LogManager.getLogManager().reset();
  115. new AndroidConfig();
  116. /*
  117. * Sets the default HTTP User-Agent used by HttpURLConnection.
  118. */
  119. String userAgent = getDefaultUserAgent();
  120. System.setProperty("http.agent", userAgent);
  121. /*
  122. * Wire socket tagging to traffic stats.
  123. */
  124. NetworkManagementSocketTagger.install();
  125. /*
  126. * If we're running in an emulator launched with "-trace", put the
  127. * VM into emulator trace profiling mode so that the user can hit
  128. * F9/F10 at any time to capture traces. This has performance
  129. * consequences, so it's not something you want to do always.
  130. */
  131. String trace = SystemProperties.get("ro.kernel.android.tracing");
  132. if (trace.equals("1")) {
  133. Slog.i(TAG, "NOTE: emulator trace profiling enabled");
  134. Debug.enableEmulatorTraceOutput();
  135. }
  136. initialized = true;
  137. }
  138. /**
  139. * Returns an HTTP user agent of the form
  140. * "Dalvik/1.1.0 (Linux; U; Android Eclair Build/MASTER)".
  141. */
  142. private static String getDefaultUserAgent() {
  143. StringBuilder result = new StringBuilder(64);
  144. result.append("Dalvik/");
  145. result.append(System.getProperty("java.vm.version")); // such as 1.1.0
  146. result.append(" (Linux; U; Android ");
  147. String version = Build.VERSION.RELEASE; // "1.0" or "3.4b5"
  148. result.append(version.length() > 0 ? version : "1.0");
  149. // add the model for the release build
  150. if ("REL".equals(Build.VERSION.CODENAME)) {
  151. String model = Build.MODEL;
  152. if (model.length() > 0) {
  153. result.append("; ");
  154. result.append(model);
  155. }
  156. }
  157. String id = Build.ID; // "MASTER" or "M4-rc20"
  158. if (id.length() > 0) {
  159. result.append(" Build/");
  160. result.append(id);
  161. }
  162. result.append(")");
  163. return result.toString();
  164. }
  165. /**
  166. * Invokes a static "main(argv[]) method on class "className".
  167. * Converts various failing exceptions into RuntimeExceptions, with
  168. * the assumption that they will then cause the VM instance to exit.
  169. *
  170. * @param className Fully-qualified class name
  171. * @param argv Argument vector for main()
  172. * @param classLoader the classLoader to load {@className} with
  173. */
  174. private static void invokeStaticMain(String className, String[] argv, ClassLoader classLoader)
  175. throws ZygoteInit.MethodAndArgsCaller {
  176. Class<?> cl;
  177. try {
  178. cl = Class.forName(className, true, classLoader);
  179. } catch (ClassNotFoundException ex) {
  180. throw new RuntimeException(
  181. "Missing class when invoking static main " + className,
  182. ex);
  183. }
  184. Method m;
  185. try {
  186. m = cl.getMethod("main", new Class[] { String[].class });
  187. } catch (NoSuchMethodException ex) {
  188. throw new RuntimeException(
  189. "Missing static main on " + className, ex);
  190. } catch (SecurityException ex) {
  191. throw new RuntimeException(
  192. "Problem getting static main on " + className, ex);
  193. }
  194. int modifiers = m.getModifiers();
  195. if (! (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) {
  196. throw new RuntimeException(
  197. "Main method is not public and static on " + className);
  198. }
  199. /*
  200. * This throw gets caught in ZygoteInit.main(), which responds
  201. * by invoking the exception's run() method. This arrangement
  202. * clears up all the stack frames that were required in setting
  203. * up the process.
  204. */
  205. throw new ZygoteInit.MethodAndArgsCaller(m, argv);
  206. }
  207. public static final void main(String[] argv) {
  208. if (argv.length == 2 && argv[1].equals("application")) {
  209. if (DEBUG) Slog.d(TAG, "RuntimeInit: Starting application");
  210. redirectLogStreams();
  211. } else {
  212. if (DEBUG) Slog.d(TAG, "RuntimeInit: Starting tool");
  213. }
  214. commonInit();
  215. /*
  216. * Now that we're running in interpreted code, call back into native code
  217. * to run the system.
  218. */
  219. nativeFinishInit();
  220. if (DEBUG) Slog.d(TAG, "Leaving RuntimeInit!");
  221. }
  222. /**
  223. * The main function called when started through the zygote process. This
  224. * could be unified with main(), if the native code in nativeFinishInit()
  225. * were rationalized with Zygote startup.<p>
  226. *
  227. * Current recognized args:
  228. * <ul>
  229. * <li> <code> [--] <start class name> <args>
  230. * </ul>
  231. *
  232. * @param targetSdkVersion target SDK version
  233. * @param argv arg strings
  234. */
  235. public static final void zygoteInit(int targetSdkVersion, String[] argv, ClassLoader classLoader)
  236. throws ZygoteInit.MethodAndArgsCaller {
  237. if (DEBUG) Slog.d(TAG, "RuntimeInit: Starting application from zygote");
  238. redirectLogStreams();
  239. commonInit();
  240. nativeZygoteInit();
  241. applicationInit(targetSdkVersion, argv, classLoader);
  242. }
  243. /**
  244. * The main function called when an application is started through a
  245. * wrapper process.
  246. *
  247. * When the wrapper starts, the runtime starts {@link RuntimeInit#main}
  248. * which calls {@link WrapperInit#main} which then calls this method.
  249. * So we don't need to call commonInit() here.
  250. *
  251. * @param targetSdkVersion target SDK version
  252. * @param argv arg strings
  253. */
  254. public static void wrapperInit(int targetSdkVersion, String[] argv)
  255. throws ZygoteInit.MethodAndArgsCaller {
  256. if (DEBUG) Slog.d(TAG, "RuntimeInit: Starting application from wrapper");
  257. applicationInit(targetSdkVersion, argv, null);
  258. }
  259. private static void applicationInit(int targetSdkVersion, String[] argv, ClassLoader classLoader)
  260. throws ZygoteInit.MethodAndArgsCaller {
  261. // If the application calls System.exit(), terminate the process
  262. // immediately without running any shutdown hooks. It is not possible to
  263. // shutdown an Android application gracefully. Among other things, the
  264. // Android runtime shutdown hooks close the Binder driver, which can cause
  265. // leftover running threads to crash before the process actually exits.
  266. nativeSetExitWithoutCleanup(true);
  267. // We want to be fairly aggressive about heap utilization, to avoid
  268. // holding on to a lot of memory that isn't needed.
  269. VMRuntime.getRuntime().setTargetHeapUtilization(0.75f);
  270. VMRuntime.getRuntime().setTargetSdkVersion(targetSdkVersion);
  271. final Arguments args;
  272. try {
  273. args = new Arguments(argv);
  274. } catch (IllegalArgumentException ex) {
  275. Slog.e(TAG, ex.getMessage());
  276. // let the process exit
  277. return;
  278. }
  279. // Remaining arguments are passed to the start class's static main
  280. invokeStaticMain(args.startClass, args.startArgs, classLoader);
  281. }
  282. /**
  283. * Redirect System.out and System.err to the Android log.
  284. */
  285. public static void redirectLogStreams() {
  286. System.out.close();
  287. System.setOut(new AndroidPrintStream(Log.INFO, "System.out"));
  288. System.err.close();
  289. System.setErr(new AndroidPrintStream(Log.WARN, "System.err"));
  290. }
  291. /**
  292. * Report a serious error in the current process. May or may not cause
  293. * the process to terminate (depends on system settings).
  294. *
  295. * @param tag to record with the error
  296. * @param t exception describing the error site and conditions
  297. */
  298. public static void wtf(String tag, Throwable t, boolean system) {
  299. try {
  300. if (ActivityManagerNative.getDefault().handleApplicationWtf(
  301. mApplicationObject, tag, system, new ApplicationErrorReport.CrashInfo(t))) {
  302. // The Activity Manager has already written us off -- now exit.
  303. Process.killProcess(Process.myPid());
  304. System.exit(10);
  305. }
  306. } catch (Throwable t2) {
  307. Slog.e(TAG, "Error reporting WTF", t2);
  308. Slog.e(TAG, "Original WTF:", t);
  309. }
  310. }
  311. /**
  312. * Set the object identifying this application/process, for reporting VM
  313. * errors.
  314. */
  315. public static final void setApplicationObject(IBinder app) {
  316. mApplicationObject = app;
  317. }
  318. public static final IBinder getApplicationObject() {
  319. return mApplicationObject;
  320. }
  321. /**
  322. * Enable debugging features.
  323. */
  324. static {
  325. // Register handlers for DDM messages.
  326. android.ddm.DdmRegister.registerHandlers();
  327. }
  328. /**
  329. * Handles argument parsing for args related to the runtime.
  330. *
  331. * Current recognized args:
  332. * <ul>
  333. * <li> <code> [--] <start class name> <args>
  334. * </ul>
  335. */
  336. static class Arguments {
  337. /** first non-option argument */
  338. String startClass;
  339. /** all following arguments */
  340. String[] startArgs;
  341. /**
  342. * Constructs instance and parses args
  343. * @param args runtime command-line args
  344. * @throws IllegalArgumentException
  345. */
  346. Arguments(String args[]) throws IllegalArgumentException {
  347. parseArgs(args);
  348. }
  349. /**
  350. * Parses the commandline arguments intended for the Runtime.
  351. */
  352. private void parseArgs(String args[])
  353. throws IllegalArgumentException {
  354. int curArg = 0;
  355. for (; curArg < args.length; curArg++) {
  356. String arg = args[curArg];
  357. if (arg.equals("--")) {
  358. curArg++;
  359. break;
  360. } else if (!arg.startsWith("--")) {
  361. break;
  362. }
  363. }
  364. if (curArg == args.length) {
  365. throw new IllegalArgumentException("Missing classname argument to RuntimeInit!");
  366. }
  367. startClass = args[curArg++];
  368. startArgs = new String[args.length - curArg];
  369. System.arraycopy(args, curArg, startArgs, 0, startArgs.length);
  370. }
  371. }
  372. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/292522
推荐阅读
相关标签
  

闽ICP备14008679号