Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Android Tutorials - สอนเขียน Android App ฟรี เขียนโปรแกรมแอนดรอยด์บน SmartPhone / Tablets > การใช้งาน Debugging (LogCat) ในการเขียน Android บนโปรแกรม Eclipse IDE (Android-Eclipse)



Clound SSD Virtual Server

การใช้งาน Debugging (LogCat) ในการเขียน Android บนโปรแกรม Eclipse IDE (Android-Eclipse)

การใช้งาน Debugging (LogCat) ในการเขียน Android บนโปรแกรม Eclipse IDE (Android-Eclipse) ในการพัฒนาโปรแกรมต่าง ๆ นั้นสิงที่สำคัญและจะช่วยให้การเขียนโปรแกรมนั้นได้ง่ายและสะดวกยิ่งขั้นก็คือการมีฟีเจอร์ที่รองรับการ Debug โปรแกรมในขณะที่โปรแกรมกำลังทำงานอยู่ในขณะนั้น ยิ่งการเขียนโปรแกรมที่มีความซับซ้อนมากขึ้น เมื่อโปรแกรมเกิดปัญหาระหว่างการทำงาน ถ้าเราสามารถที่จะ Debug ทีล่ะ Step เพื่อตรวจสอบค่าตัวแปรและฟังก์ชั่นต่าง ๆ ก็จะทำให้การหาสาเหตุนั้นสามารถทำได้ดียิ่งขึ้น และเช่นเดียวกันการเขียนโปรแกรม Adroid บน Eclipse IDE ถ้ามีการลง Plugin ของ ADT ก็จะสามารถใช้ฟีเจอร์ความสามารถนี้เช่นเดียวกัน และจากที่ได้ลองใช้ดูแล้ว สามารถทำงานและ Debug ค่าต่าง ๆ ได้เป็นอย่างดีเช่นเดียวกัน


Android Eclipse Debugging


อธิบายเพิ่มเติมก่อนว่าใน ADT Plugin ที่เขียนบน Eclipse จะมี LogCat ซึ่งจะทำหน้าที่เก็บ Process หรือ Log ต่าง ๆ ที่เกิดขึ้นในช่วงระหว่างการรันโปรแกรม ทั้งหมด ซึ่งเราสามารถเรียกดู LogCat ได้ที่

LogCat
Windows -> Show View -> Other... -> Android -> LogCat


Android Eclipse Debugging

จากรูปจะเห็นว่า LogCat ซึ่งมี Column แรกชื่อว่า Level และตามด้วย Time และอื่น ๆ และที่สำคัญคือ Tag และ Text ซึ่งจะระบุว่า Process บรรทั้ดนั้น ๆ คืออะไร และมี Text แจ้งว่าอะไร โดย Log เหล่านี้มีทั้งที่เป็น Debug และ Warning ต่าง ๆ โดยเรามาดูตัวย่อของแต่ล่ะ Level

Level
V — Verbose (lowest priority)
D — Debug
I — Info
W — Warning
E — Error
F — Fatal
S — Silent (highest priority, on which nothing is ever printed)


เหตุผลที่เราจะต้องสนใจ LogCat ก็คือในระหว่างที่โปรแกรมทำงานอยู่เราจะสามารถตรวจสอบค่าหรือ Error ต่าง ๆ ได้จากในนี้ และนอกจาก Error หรือ Log จากระบบอัตโนมัติแล้ว เรายังสามารถเขียน Log ของเราเองลงในโปรแกรมได้อีกด้วย โดยใช้ Library ของ android.util.Log โดยมีรูปแบบคือ

Log.d(TAG_NAME,TEXT_MESSAGE);


โดยถ้าเราแทรกคำสั่งเหล่านี้ลงใน Code ถ้ามีการทำงานก็จะมีการเขียน Log ลงใน LogCat ด้วย

สำหรับบทความนี้จะอธิบายวิธีการเขียน LogCat และการ Debug โปรแกรมอย่างลาะเอียด ซึ่งจะอธิบายทั้ง 2 วิธี โดยวิธีแรกจะเป็นการเขียนลงใน LogCat


1. การเขียน Log ลงใน LogCat

ในตัวอย่างนี้จะเป็นการยกตัวอย่างการเขียน LogCat ลงในตัวแปร i ซึ่งมีค่าเริ่มค้นจาก 0 และมีปุ่มอยู่ 2 ปุ่มคือ
Button1 เมื่อคลิกให้ i = i + 1;
Button2 เมื่อคลิกให้ i = i + 2;

Android Eclipse Debugging

ออกแบบหน้าจอดังรูป

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="i = 0" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="42dp"
        android:text="Button1 (i = i + 1)" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="25dp"
        android:text="Button2 (i = i + 2)" />

</RelativeLayout>


ตัวนี้โครงสร้างไฟล์ XML








MainActivity.java
package com.myapp;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.util.Log;

public class MainActivity extends Activity {
	
	int i =0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		// textView1
		final TextView txtView1 = (TextView) findViewById(R.id.textView1);
		
		// button1 (i = i + 1)
        final Button btn1 = (Button) findViewById(R.id.button1);
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                i = i + 1;
                Log.d("Step 1","i = " + i);
                
                txtView1.setText("i = " + i); // Show on textView1
            }
        });
        
		// button2 (i = i + 2)
        final Button btn2 = (Button) findViewById(R.id.button2);
        btn2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                i = i + 2;
                Log.d("Step 2","i = " + i);
                
                txtView1.setText("i = " + i); // Show on textView2
            }
        });
	}
	

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}


อันนี้ Code ของ Java ที่จะใช้ทดสอบ

คำอธิบาย

Android Eclipse Debugging

จาก Code จะเห็นว่ามีการประกาศตัวแปร i = 0; มี Event ของ Button1 และ Button2 โดย
Button1 เมื่อคลิกจะเปลี่ยนค่า i = i + 1; และเขียนลงใน LogCat
Button2 เมื่อคลิกจะเปลี่ยนค่า i = i + 2; และเขียนลงใน LogCat

ซึ่งจากตัวอย่างผลที่ได้คือเมื่อโปรแกรมเริ่มต้น จะแสดงค่า 0 และคลิก Button1 จะได้ค่า i = 1 และคลิก Button2 จะได้ค่า i = 3

ทดสอบการรันโปรแกรมบน Emulator

Android Eclipse Debugging

เมื่อเริ่มต้นจะเห็นว่าตัวแปร i = 0

Android Eclipse Debugging

คลิกที่ Button1 ค่าตัวแปร i = 1

Android Eclipse Debugging

คลิกที่ Button2 ค่าตัวแปร i = 3








การดู LogCat
ที่นี่เรามาดู LogCat ที่เราได้เขียนลงไป

Android Eclipse Debugging

LogCat
Windows -> Show View -> Other... -> Android -> LogCat


Android Eclipse Debugging

เลือก LogCat

Android Eclipse Debugging

จาก LogCat ใน Level : D จะเห็นว่ามี Log ที่เราเขียนไป 2 รายการ และค่า Tag กับ Text ที่เขียนลงไป


2. วิธีการ Debug โปรแกรมแบบละเอียด
วิธ๊นี้ถือได้ว่าเป็นฟีเจอร์เด่นของ ADT ที่ทำงานบน Eclipse เลยซะทีเดียว เพราะสามารถตรวจสอบค่าตัวแปรได้แบบละเอียดยิบ และสามารถสั่งให้โปรแกรมทำงานไปตาม Step ที่ต้องการได้

Android Eclipse Debugging

ขั้นแรกคือให้ไปที่ Code ที่เป็น Java ให้คลิกขวาตำแหน่งที่ต้องการให้โปรแกรมหยุดการทำงาน หรือ Breakpoint โดยคลิกขวในตำขวาและเลือก Toggle Breakpoint ดังรูป

Android Eclipse Debugging

เราจะลองทำการกำหนด Breakpoint ในตำแหน่ง 3 จุด เพื่อตรวจสอบค่าตัวแปรแต่ล่ะ Step โดย
ตำแหน่ง 1 เป็นช่วงที่โปรแกรมเริ่มต้น (i = 0)
ตำแหน่ง 2 เมื่อคลิกที่ Button1 (i = i + 1)
ตำแหน่ง 3 เมื่อคลิกที่ Button2 (i = i + 2)

Android Eclipse Debugging

จากนั้นให้ไปที่เมนู Run -> Debug หรือ (F11)

Android Eclipse Debugging

ขั้นตอนนี้ให้เลือก OK ไปได้เลย

จากนั้นโปรแกรมก็จะทำการรันไปซะพักหนึ่ง ในช่วงนี้ให้รอ อาจจะใช้เวลานานแต่ไม่เกิน 1 นาที ขึ้นอยู่กับตำแหน่งของ Breakpoint

Android Eclipse Debugging

เมื่อโปรแกรมทำงานมาถึงจุด Breakpoint ตำแหน่งที่ 1 เราจะตรวจสอบค่าตัวแปรต่าง ๆ ได้โดยการดับคลิกที่ Variables

Android Eclipse Debugging

เมื่อคลิกที่ this เราก็จะเห็นตัวแปรมากมาย พร้อมกับค่าตัวแปรต่าง ๆ ด้วย ถ้าไม่ต้องการทำอะไรแล้วก็ให้ปิดหน้าตอนี้โดยการ ดับคลิกที่ Variables อีกครั้ง

Android Eclipse Debugging

จากนั้นเราจะไป Breakpoint ตำแหน่ง 2 โดยการคลิกที่ Resume หรือ (F8)

Android Eclipse Debugging

เมื่อกด F8 โปรแกรมก็จะแสดงหน้าแรก เพราะ Breakpoint ตำแหน่ง 2 จะทำงานเมื่อคลิกที่ Button1

Android Eclipse Debugging

จะเห็นว่า Breakpoint มาหยุดอยู่ ตำแหน่ง 2 เราสามารถดูค่าตัวแปรได้เช่นเดิม

Android Eclipse Debugging

จะเห็นว่าค่า i ได้เปลี่ยนไปแล้วจาก i = i + 1 (ในขณะนี้ i มีค่าเป็น 1)

Android Eclipse Debugging

หรือจะลองใช้ Expressions เพื่อตรวจสอบค่าตัวแปรต่าง ๆ

Android Eclipse Debugging

ให้พิมพ์ชื่อค่าตัวแปรเข้าไปได้เลย ในฝั่งซ้ายเป็นชื่อตัวแปร ส่วนฝั่งขาวจะเป็นค่าตัวแปรที่ได้

Android Eclipse Debugging

ไปยัง Breakpoint ตำแหน่ง 3

Android Eclipse Debugging

คลิกที่ Button2

Android Eclipse Debugging

Breakpoint มาหยุดอยู่ใน ตำแหน่ง 3 สามารถดูค่าตัวแปรได้เช่นเดียวกัน

Android Eclipse Debugging

จะเห็นว่า i มีค่าเป็น 3 (เพราะก่อนหน้านี้ i = 1 เมื่อเข้า Button2 จะบวก 2 เลยได้ค่าเป็น 3)

Android Eclipse Debugging

ไปยัง Step ถัดไปโดยคลิกที่ Resume ถ้าไม่มี Breakpoint ในตำแหน่งถัดไปโปรแกรมก็จะทำงานจนสิ้นสุด

Android Eclipse Debugging

หลังจากสิ้นสุดจะเห็นว่า i มีค่าเป็น 3

สำหรับการ Debug โปรแกรม Android ผ่าน Eclipse ก็มีเพียงง่าย ๆ เท่านี้ ถ้าต้องการใช้ความสามารถอื่น ๆ ก็สามารถลองผิดลองถูกได้จากหลาย ๆ เมนู ซึ่งจะเป็นการฝึกประสบการณ์ในการเขียนโปรแกรมได้อย่างดี



เพิ่มเติม
กรณีที่ต้องการกลับไปยัง Layout หรือ Windows ปกติให้คลิกไปที่เมนู

Android Eclipse Debugging

Windows -> Open Perpective -> Java


Android Eclipse Debugging

อันนี้เป็น LogCat ที่เกิดจึ้นระหว่างการ Debug สามารถดูค่าได้เช่นเดียวกัน

Android Eclipse Debugging

ในระหว่างที่โปรแกรม Debug อยู่จะสามารใช้ฟีเจอร์ต่าง ๆ ได้จากดังภาพ

สำหรับที่จะใช้งานบ่อย ๆ ก็มีไม่กี่ตัวคือ
F5 ทำการ Debug ทุกอย่าง เข้าไปใน method ย่อย ๆ ด้วย
F6 ไม่เข้าไปใน method ย่อย
F8 ข้าม Breakpoint นั้นไปเลย เพื่อไป Breakpoint ตัวถัดไป หรือโปรแกรมสิ้นสุด


Android Eclipse Debugging

เช่น Skip all Breakpoint

Android Eclipse Debugging

Resume (F8)

Android Eclipse Debugging

Step Info (F5)

Android Eclipse Debugging

Step Over (F6)

Android Eclipse Debugging

Step Return (F7)

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-07-03 17:37:47 / 2017-03-26 20:33:26
  Download : No files
 Sponsored Links / Related

 
Android File Structure โครงสร้างของไฟล์ของ Android บน Smart Phone และ Tablets
Rating :

 
รู้จัก Palette หรือเครื่องมือ Widgets Element ต่าง ๆ ที่อยู่ใน Android และ Eclipse
Rating :

 
Android สร้าง Launcher Icons และ Title Bar บน Eclipse และ ใช้งาน Emulator เบื้องต้น
Rating :

 
พื้นฐานการเขียน Android กับโปรแกรมภาษา Java Syntax และการใช้งาน Eclipse เบื้องต้น
Rating :

 
ฟรี!! อบรม Android เรียน Android จะเขียน แอนดรอยด์ แหล่งความรู้แบบ ฟรี ๆ
Rating :

 
Android Change Launcher Icons การเปลี่ยนไอคอนของโปรแกรมทีเขียนบนแอนดรอยด์
Rating :

 
การแก้ปัญหาเบื้องต้นเมื่อ Emulator ไม่ตอบสนอง ไม่สามารถรันโปรแกรม Android ได้
Rating :

 
Android - Process system is not responding / has stopped / unexpectedly
Rating :

 
การเซ็ตให้ Eclipse - Run / Debug โปรแกรม Android บน Device และ Smart Phone หรือ Tablets จริง
Rating :

 
Android Export APK การนำ App ที่เขียนบน Eclipse ทำเป็น .apk ไป Install ใช้งานจริงบน Smart Phone และ Tablets
Rating :

 
Android Sample Project ของ Android SDK ที่อยู่ใน Eclipse มีอะไรดีและน่าสนใจกว่าที่คิด
Rating :

 
Android Rotate Switching Portrait and Landscape - Emulator (มุมมองในแนวตั้งและแนวนอน)
Rating :

 
Android การทำ Splash Screen Loading ในช่วงระหว่างการเข้าสู่ App หรือระหว่างในช่วงรอการเปลี่ยนหน้า
Rating :

 
Android External Font Face / Font Color / Font Size
Rating :

 
Android Import Jar Library
Rating :

 
Android get current Date/Time
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่