Tutorial don koyon yadda za a dafa your Android aikace-aikace tare da Logcat da Android Studio

I. Gabatarwa

Babu makawa, idan muka ci gaba muna yin kwari. Ba wanda ya tsira, har ma mafi kyau. Saboda haka yana da muhimmanci a sami damar ganowa da fahimtar kwari don magance su.

Android Studio na samar da dukkan kayayyakin aiki, don cire kuskure your code. Wannan labarin za a iyakance zuwa Java code, amma san cewa, yana yiwuwa ya cire kuskure code C ko C ++ da Kotlin.

Za mu tattauna kawai game da amfani da Logcat wanda shine mataki na farko na debugging. Hakanan zaka iya ci gaba tare da tsayawa maki da matakan mataki-by-step.

II. Abinda ake bukata

Don haɓaka shi yana buƙatar na’urar da ta ba shi damar. A wayarka / kwamfutar hannu za ka buƙaci shiga cikin zabin masu tasowa kuma don ba da damar USB na debugging.

Babu hotuna

Idan kayi amfani da emulator, babu wani abu da za a yi, duk abin da aka bayar ta tsoho.

Lokacin ƙirƙirar aikin Gidan Ayyuka na yau dole ne ka ƙirƙiri ta atomatik bambancin aikace-aikacenka. Ba a bayyane yake a cikin fayil ɗin gwargwadon kwamfuta ba , amma yana nan.

III. Karanta kuma rubuta jaridu

Babban mahimmanci don gano bayanin lalacewa shine Logcat. Wannan wani ra’ayi ne na Kamfanin na Android da ke ba ka damar ganin duk ɗakunan da tsarin da aikace-aikace suka rubuta.

Ana iya samun dama ta hanyar shafuka a kasan allon ko ta hanyar gajeren hanyar « Alt + 6 »

Babu hotuna

III-A. Karanta

Akwai bayanai mai yawa a cikin Logcat, kuma mafi yawansu ba su damu da mu ba. Saboda haka wajibi ne don tace saƙonnin kamar yadda aka tsara:

  1. Na’urar da aka damu;
  2. Shirin ya shafi;
  3. Girman sakon;
  4. Tsarin rubutu na rubutu
  5. Yanayin jaridu.
Babu hotuna

Manufar ita ce ganin kawai saƙonni game da aikace-aikacen da kake son buguwa da kome ba.

Da zarar ana amfani da filtaniya, dole ne mu fahimci abin da muke gani. Abin farin duk saƙonnin a cikin Logcat suna da nau’i daya:

[kwanan wata] [lokaci] [PID-TID / kunshin] [girma / tag] [sakon]

Alal misali:

11-13 13: 02: 50.071 1901-4229 / com.google.android.gms V / AuthZen : Karɓar wakilin wakilai.

Menene wannan sakon ya gaya mana?

Ranar 13 ga Nuwamba a 1:02 pm aikace-aikacen com.google.android.gms ya nuna saƙon « Karɓar wakilin wakilai. Daga ƙwararren verbose tare da lambar AuthZen (lakabi) .

Zaka kuma lura da launuka daban-daban. Kowace launi yana wakiltar sakonnin sakonnin tsakanin verbose, debug, info, gargadi da kuskure. Abu mai sauƙi ka tuna: lokacin da yake ja, ba alama ce mai kyau ba!

III-B. Rubuta

Yanzu da cewa mun san yadda za mu karanta Logcat, za mu yi amfani da amfani da shi don nuna saƙonninmu.

Wannan ƙwarewar hanya ce mafi sauki, wanda shine don ƙara « alamu » a cikin lambar don duba darajar misali.

Rubutun ga Logcat an yi ta amfani da layi na Log ( https://developer.android.com/reference/android/util/Log.html )

Yana ba da damar rubuta saƙo a yanayin da ake bukata. Misali don rubuta bayanai:

Saƙon bayani

zabi
Log.i(tag,message) ;

don saƙon sakonni:

Jagorar Debug

zabi
Log.d(tag,message) ;

Da dai sauransu.

Alamar tag ita ce igiya mai kyauta; ta hanyar tarurruka, yawanci ana danganta shi a cikin suna na yanzu, wanda ya ba da damar sanin da sauri inda sakon yake fitowa.

A saitin Message halin yanzu shi ne sakon kana so ka nuna.

Don haka alal misali lambar da ta biyo baya:

Misali misali

zabi
int age = 8; Log.d("MonActivity","Mon age est de "+age);

Zai nuna:

sakamakon

zabi
11-16 13:00:38.856 4580-4580/com.demo.debug D/MonActivity: Mon age est de 8.
Babu hotuna

IV. Binciken fasalin burbushin

IV-A. Sanya alama?

Yanzu da ka yi amfani da Logcat, ya kamata ka sami abin da ake kira tarin tari . Sakamakon tasirin shi ne saƙon kuskuren kuskure wanda yake gaya maka wani matsala ta musamman da yadda ya faru. Tare da lakabin tari da kake cikin ka’idar iya sanin wane layi na lambarka yana haifar da matsaloli da kuma yadda aka kira wannan lambar.

IV-B. Analysis

Don manufar wannan labarin, zan yi kuskuren ƙirƙirar kuskure: Ina ƙoƙarin amfani da widget ɗin da bai wanzu a cikin shimfidawa da aka ɗora ta ta aiki na:

Misalan misalin

zabi
Button btn = findViewById(R.id.existepas); btn.setText("Test");

Sakamakon shi ne hadari a matakin aikace-aikacen:

Babu hotuna

Kuma babu shakka, zamu sami alamar tari a cikin Logcat:

11-20 14: 27: 56.159 4837-4837 / com.demo.debug E / AndroidRuntime: FATAL EXCEPTION: main

Tsari: com.demo.debug, PID: 4837

Sanya alama

zabi
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.demo.debug/com.demo.debug.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
      at android.app.ActivityThread.access$800(ActivityThread.java:151)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5254)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference
      at com.demo.debug.MainActivity.onCreate(MainActivity.java:16)
      at android.app.Activity.performCreate(Activity.java:5990)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5254) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

Akwai bayanai da yawa a cikin wannan alama:

Mun ga cewa muna hulɗar da wani RuntimeEception, musamman cewa tsarin ba zai iya ɗaukar aiki na MainActivity ba:

zabi
Unable to start activity ComponentInfo{com.demo.debug/com.demo.debug.MainActivity}

Mun ga dama bayan mun fuskanci java.lang.NullPointerException .

Babu wani abu a wannan lokacin da zai ba mu damar gano tushen matsalar …

Sai dai idan muka sauka kadan a cikin hanyar, zamu sami layin da ke biyewa:

Lalacewa ta hanyar : java.lang.NullPointerException: Ƙoƙari kira mai rumfa Hanyar ‘wõfintattu android.widget.Button.setText (java.lang.CharSequence)’ ya null abu reference

Layin da aka yi ta ya gaya mana abin da ke tushen kuskure. A nan NullPointerException lokacin da ake kiran saitinTaxt (), kuma idan muka sauka kadan a cikin yanayin an gaya mana ainihin wuri:

a com.demo.debug. MainActivity.onCreate (MainActivity.java 16 )

Za ku lura cewa ana iya amfani da rubutu a cikin iyayengiji, wanda zai kai ku kai tsaye zuwa wurin da ke daidai a cikin lambar. Bugu da ƙari, idan lambar da ke damuwa naka ne, za a nuna rubutu a blue ba kamar sauran fayilolin da za a yi musu ba.

Saboda haka aikin MainActivity onCreate () yana haifar da matsala, musamman layin 16.

Sa’an nan kawai ka je code don ganin layin da ake tambaya:

zabi
btn.setText("Test");

Sabili da haka zamu iya cewa cewa « btn » mai ma’ana ba kome ba ne, wanda ke haifar da kuskure.

Don cire kuskure, dole ne a gyara wannan matsala.

A cikin yanayinmu, muna da mafita biyu, wato na gwaji kafin yin amfani da madaidaiciya btn:

zabi
if(btn!= null) btn.setText("Test");

Ko dai ku tabbata cewa ra’ayi da ake nema findViewById() ya wanzu.

IV-C. Mafi girma

NullPointerException

Babu shakka kuskure mafi kuskuren Java, wanda ke nufin cewa muna ƙoƙarin amfani da ƙwayar null.

Magani: Bincika ƙarancin m kafin amfani da shi.

ActivityNotFoundException

Wannan kuskure yana faruwa a yayin ƙoƙarin kira wani aiki ta faraActivity (), amma wannan aikin ba a bayyana shi a fili ba .

Magani: bayyana aikin a fili .

ClassCastException

Wannan kuskure yana faruwa a lokacin ƙoƙarin kaddamar da wani abu a cikin wani misali misali mara dace. Alal misali, ƙaddamar da TextView a View yana da kyau. A gefe guda don kaddamar da TextView a cikin Intanet zai haifar da kuskure.

Magani: Kullum gudu a cikin dama.

NetworkOnMainThreadException

A cikin Android, an haramta shi don ƙirƙirar tambayoyin cibiyar sadarwa a kan layin UI. Idan kunyi haka, za ku sami wannan kuskure.

Magani: Yi buƙatun cibiyar sadarwa a cikin asynctask ko a cikin rabaccen launi.

Ayyukan Ayyukan Ayyukan Ginin Da Aka Tunada A nan

Wannan kuskure yana faruwa sau da yawa lokacin da mutum yayi ƙoƙarin nuna wani maganganu bayan barin aikin da aka haɗa shi.

Magani: Kullum halakar maganganun a kan Pause () ko Done ().

Soyez le premier à commenter

Poster un Commentaire

Votre adresse de messagerie ne sera pas publiée.


*