致命傷を避けるための例外実例集

 致命傷を避けるための例外実例集

Bbe9718bebdafbdc8dabbe3cadf1bc46?s=128

Keishin Yokomaku

November 09, 2016
Tweet

Transcript

  1. க໋ইΛආ͚ΔͨΊͷ ྫ֎࣮ྫू Keishin Yokomaku / Startup Android #1

  2. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू About Me ▸ Keishin Yokomaku ▸ Drivemode, Inc. /

    Principal Engineer ▸ KeithYokoma: GitHub / Twitter / Qiita / Tumblr / Stack Overflow ▸ Books: Mobile App Dev Guide / Android Academia / Grimoire of Android ▸ Fun: Gymnastics / Cycling / Photography / Motorsport ▸ Today’s Quote: “࣌୅͸ے೑” 2
  3. ྫ֎ 3

  4. 4

  5. 5 ˒ˑˑˑˑ This app keeps crashing. Uninstalled.

  6. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू ই͸ઙ͍͏ͪʹ࠹͙ ▸ ϦϦʔεઓུͱΫϥογϡݕ஌ͷ࢓૊Έ͸ඞਢςΫχοΫ ▸ Fabric(Crashlytics) ͰΫϥογϡϩάͷऩू ▸ Google Play

    Developer Console Ͱஈ֊తϦϦʔε ▸ ϢʔβϑΟʔυόοΫͷมԽʹؾ෇͘ ▸ ʮ࠷ۙ͜ΜͳϑΟʔυόοΫ͕Α͘དྷͯΔʯ ▸ ʮ͋ͷϑΟʔυόοΫݮͬͨʯ 6
  7. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू ෆ৚ཧͱઓ͏ ▸ ໌Β͔ʹΫϥογϡ͢Δͱ෼͔Δ΋ͷ͚͕ͩݪҼͰ͸ͳ͍ ▸ Ϣʔβͷ࢖༻ঢ়گʹΑͬͯΫϥογϡͨ͠Γ͠ͳ͔ͬͨΓ͢Δ΋ͷ΋͋Δ ▸ ୺຤ͷҧ͍ɺ࢖͍ํͷҧ͍ɺଞʹΠϯετʔϧ͍ͯ͠ΔΞϓϦͷҧ͍ ▸ ༷ʑͳཁҼ͕༧૝֎ͷΫϥογϡΛ༠ൃ͢Δ

    ▸ ͱ͖ʹ͸ݟ஌Β͵ྫ֎͕ݪҼͰ͋Δ͜ͱ΋͋Δ ▸ ྫ֎ͷ໊લ΍ϝοηʔδ͔Βঢ়گ΍ཧ༝ΛಡΈऔΔεΩϧΛ਎ʹண͚Α͏ 7
  8. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू Let’s get started! ▸ ຊ೔ͷྫ֎Ϩγϐ ▸ ༗໊Ͳ͜Ζ͔ΒͲϚΠφʔͳ΋ͷ·ͰϞϦϞϦίʔε 8 NullPointerException

    OutOfMemoryError StackOverflowError Illegal**Exception CursorWindowAllocationException TransactionTooLargeException DeadObjectException
  9. NullPointerException 9

  10. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू NullPointerException ▸ Ұ൪Α͋͘ΔΫϥογϡͷݪҼ ▸ ى͖Δཧ༝͸༷ʑ ▸ ίʔυ্Ͱͷݕࠪ࿙Ε ▸ Nullable

    Ͱͳ͍৔ॴʹ null Λ୅ೖ ▸ ΦϒδΣΫτͷϥΠϑαΠΫϧʹର͢Δߟྀ࿙Ε 10
  11. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू NullPointerException ▸ Ή΍ΈʹରࡦΛऔͬͯ͸͍͚ͳ͍ ▸ e.g. ͋ΒΏΔϝιουͰ null νΣοΫΛೖΕΔ ▸

    ๷ޚతʹͳΓ͗͢ΔͱޙʑͷϝϯςφϯεͰ଍ΛҾͬுͬͯ͠·͏ 11
  12. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू NullPointerException ▸ ରࡦͷํ๏ ▸ ઃܭʹΑΔରࡦ ▸ ϝιου͕ null Λڐ༰͢Δ͔Ͳ͏͔ද໌͢Δ͜ͱͰɺ


    ୭͕null νΣοΫͷ੹຿Λෛ͏͔ܾΊΔ ▸ NullObject ύλʔϯΛ࢖ͬͯɺԿ΋ى͖ͳ͍Α͏ʹ޻෉͢Δ 12
  13. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू NullPointerException ▸ ରࡦͷํ๏ ▸ ϑϨʔϜϫʔΫͷίʔυͷ೺Ѳ ▸ Ͳ͏͍͏ঢ়گͰϝιου͕ null Λฦ͔͢஌͓ͬͯ͘

    ▸ Support Library ͷ࣮ଶΛ஌͓ͬͯ͘ ▸ ۭ࣮૷ͷ··ϦϦʔε͞Ε͍ͯΔ͕࣌͋Δ ▸ ϑϨʔϜϫʔΫͷ࡞๏ʹ४ͨ͡࡞ΓํΛ֮͑Δ 13
  14. OutOfMemoryError 14

  15. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू OutOfMemoryError ▸ ը૾΍ಈըͳͲେ͖ͳσʔλΛѻ͏ͱඞͣ໨ʹ͢Δྫ֎ ▸ จࣈ௨Γɺۭ͖ͷͳ͍ώʔϓʹΦϒδΣΫτΛ࡞Ζ͏ͱ͍ͯ͠Δͱ͖ʹى͖Δ ▸ ରࡦํ๏͸ΘΓͱ໌շ ▸ େ͖ͳΦϒδΣΫτΛখ͘͢͞Δ

    ▸ ಉ࣌ʹͨ͘͞ΜͷΦϒδΣΫτΛώʔϓʹೖΕͳ͍ ▸ ϝϞϦϦʔΫΛͳ͘͢ 15
  16. StackOverflowError 16

  17. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू StackOverflowError ▸ ελοΫΛ࢖͍௵ͨ͠ͱ͖ʹى͖Δྫ֎ ▸ e.g. ແݶ࠶ؼݺͼग़͠ ▸ ݟམͱ͕ͪ͠ͳՕॴ ▸

    ਂ͗͢ΔϨΠΞ΢τͷωετ: ϨΠΞ΢τ΋݁ہ͸ Java ͷΦϒδΣΫτʹͳΔ ▸ ਂ͗͢Δґଘؔ܎: ա৒ͳϨΠϠԽͰϝιουͷίʔϧελοΫʹݶք͕͘Δ 17
  18. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू StackOverflowError ▸ ରࡦํ๏ ▸ ϨΠΞ΢τͷ֊૚ߏ଄ΛݮΒ͢ ▸ RelativeLayout ΍ ConstraintLayout

    ͳͲͰϑϥοτͳߏ଄ʹ͢Δ ▸ ద੾ͳཻ౓ͰઃܭΛ͢Δ ▸ Clean Architecture ΍ DDD ͳͲΛࢀߟʹ 18
  19. Illegal**Exception 19

  20. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू Illegal**Exception ▸ छྨ͸৭ʑ ▸ IllegalStateException, IllegalArgumentException, etc… ▸ ݺͼग़͞ΕΔଆ͕૝ఆ͍ͯ͠ͳ͍ঢ়ଶɾҾ਺ʹ௚໘ͨ͠ͱ͖ʹى͖Δ

    ▸ ݺͼग़͢ଆ͕Ҿ਺νΣοΫ΍ঢ়ଶνΣοΫΛ͢Δ͜ͱΛཁٻ͍ͯ͠Δ ▸ ୺຤ґଘͰެࣜͷ Javadoc ʹॻ͍ͯͳ͍ͱ͖ʹඈΜͰ͘Δ͜ͱ͕͋Δ ▸ ո͍͠ͳʁͱࢥͬͨΒ͙͢ʹঢ়ଶ΍Ҿ਺ΛϩάʹͭΊͯूΊΒΕΔ࢓૊ΈΛ 20
  21. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू Illegal**Exception ▸ ରࡦํ๏ ▸ ͙͢ʹ௚ͤΔ΋ͷ͹͔ΓͰ͸ͳ͍͜ͱʹཹҙ͢Δ ▸ ඞཁͳ৘ใΛूΊɺରࡦΛ࿅͔ͬͯΒ௚͢͜ͱ ▸ Crashlytics

    ͷϩάऩूػೳͰࣄલ৚݅ΛνΣοΫ͢ΔͱΑ͍ 21
  22. CursorWindowAllocation Exception 22

  23. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू CursorWindowAllocationException ▸ σʔλϕʔεΛѻ͏ͱ໨ʹ͢Δྫ֎ ▸ Cursor ͕΋ͭσʔλ͕ڐ༰ൣғΛ௒͑ͨͱ͖ʹى͖Δ ▸ ϦϑΝϨϯεʹ Javadoc

    ͕ແ͍ྫ֎͕ͩɺࡶʹ࡞Δͱ໨ܸ͢Δϋϝʹ ▸ CursorWindow ΫϥεͷதͰϋϯυϦϯά͍ͯ͠Δ ▸ CursorWindow.java: http://bit.ly/2eKlL5y 23
  24. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू CursorWindowAllocationException ▸ ରࡦํ๏ ▸ ҰؾʹେྔͷσʔλΛऔಘ͍ͯ͠Δ෦෼Λࡉ੾Εʹ͢Δ ▸ σʔλϕʔεʹڊେͳσʔλΛอ؅͠ͳ͍ ▸ ࢖͍ऴΘͬͨ

    Cursor ͸ close ͢Δ 24
  25. TransactionTooLarge
 Exception 25

  26. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू TransactionTooLargeException ▸ ϓϩηεؒ௨৴ͰϝϞϦΛ࢖͍͗ͨ͢ͱ͖ʹى͖Δྫ֎ ▸ 1ͭͷϓϩηεʹରׂͯ͠Γ౰ͯΒΕΔϓϩηεؒ௨৴༻ͷྖҬ͸1mB ▸ ҙ֎ͳͱ͜ΖͰϓϩηεؒ௨৴͸࢖ΘΕ͍ͯΔ ▸ Context#startActivity

    ▸ PackageManager#queryIntentActivities ▸ ଞଟ਺ 26
  27. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू TransactionTooLargeException ▸ Intent ͷ extra ʹ 1mB Λ௒͑Δ String

    Λ౉͢͜ͱͰ΋ى͖Δఔ౓ʹ͸਎ۙͳͱ ͜ΖʹՄೳੑ͕͋Δ ▸ ରࡦํ๏ ▸ ڊେͳσʔλΛ௚઀ѻΘͳ͍ ▸ ϓϩηεؒ௨৴ͷ࣌ؒΛ୹͘͢Δ ▸ ϓϩηεؒ௨৴ͷճ਺ͦͷ΋ͷΛݮΒ͢ 27
  28. DeadObjectException 28

  29. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू DeadObjectException ▸ ࢮΜͩϓϩηεʹ࿩͔͚ͨ͠ͱ͖ʹى͖Δྫ֎ ▸ ௨৴૬खͷϓϩηε͕͍ͭ΋ੜ͖͍ͯΔͱ͸ݶΒͳ͍ ▸ ରࡦํ๏ ▸ ServiceConnection

    ΍ IBinder.DeathRecipient Ͱࢮ׆؂ࢹ ▸ ڊେͳσʔλΛ௨৴ʹͷͤͳ͍ ▸ System Service ͸ࢮ׆؂ࢹͰ͖ͳͦ͞͏…… 29
  30. ·ͱΊ 30

  31. 31

  32. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू ·ͱΊ ▸ ରࡦํ๏ͦͷ΋ͷʹֵ৽తͳ͜ͱ͸΄ͱΜͲແ͍ ▸ Α͋͘Δ࣮ྫΛͦͷ··ద༻Ͱ͖Δ͜ͱ΋ଟ͍ ▸ ߟ͑ํͷجຊ ▸ ॲཧͷ୯Ґ΍σʔλΛখ͘͞ࡉ͔͘͢Δ

    ▸ ઃܭͱද໌Ͱ੹຿Λ໌Β͔ʹ͢Δ 32
  33. க໋ইΛආ͚ΔͨΊͷྫ֎࣮ྫू ·ͱΊ ▸ ͍͔ʹૣ͘৘ใΛूΊͯରࡦ͕औΕΔ͔͕ॏཁ ▸ ஈ֊తϦϦʔε ▸ Ϋϥογϡϩάͷऩू ▸ ϢʔβϑΟʔυόοΫͷղੳ

    33
  34. க໋ইΛආ͚ΔͨΊͷ ྫ֎࣮ྫू Keishin Yokomaku / Startup Android #1

  35. DroidKaigi Registration starts from mid-November! 35