Advertisement
yo2man

Logs (kinda like toasts except for errors)

Jun 22nd, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. //Logs (kinda like toasts except for errors)
  2. the Log class:
  3.  
  4. // Another common way to investigate our app while it's running is to use the Android log.
  5. // We can add calls to different log methods that will write data to the Android system log.
  6. // We can then view the log in our IDE using logcat while the app is running or
  7. // even at a later time.
  8. // Even if our app crashes, the log remains,
  9. // often holding helpful information about why the app crashed.
  10.  
  11. // We write to the log using the Log class and then one of its static methods.
  12. // So it's kind of similar to what we had with Toast.makeText.
  13.  
  14. //The static methods don't return anything, so
  15. // we don't need to store them in a variable, just like we didn't need to when we chained the show method at the end of the toast.
  16.  
  17.  
  18. Log.e() //E is for errors
  19. Log.w() //W is for warnings
  20. Log.i() //i is for informational entries
  21. Log.d() //d is for debugging during development
  22. Log.v() //v is for verbose, which are used when we're troubleshooting something and we want to spew as much information as possible to the log.
  23.  
  24. Step 1: public static final String TAG = "FunFactsActivity"; // Static final means this tag constant is always available and will never change //this is Java's way of saying don't change this ever.
  25.  
  26. Step 2. : public static final String TAG = FunFactsActivity.class.getSimpleName(); // remove quotes from "FunFactsActivity" to make it not a string and then add '.class.getSimpleName(); //what this basically does is instead of a hardcoded "String" we can use a special method to get the class name automatically. This way the refactoring tools will recognize it and make the change if we ever decide to change the name of 'FunFactsActivity'. EASY. *
  27.  
  28.  
  29.  
  30. Step 1. Log.d("FunFactsActivity", "We're Logging from the onCreate() method!");
  31.  
  32. Log.d (String tag, String msg)
  33. // the first parameter is what is known as a tag, also a string.
  34. // The tag is used to identify the source of a log message. It usually identifies the class or activity where the call to log occurs.
  35.  
  36. // The second parameter is the string data that we want to write to the log.
  37.  
  38.  
  39. Step 2:  Log.d(TAG, "We're Logging from the onCreate() method!"); //again change "FunFactsActivity" to TAG. EASY. *
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. // https://teamtreehouse.com/library/build-a-simple-android-app/testing-and-debugging/the-android-log
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement