Weird Dalvik format failed error

Strange Android error I came across tonight(or should I say this morning :s).

[2009-10-13 03:18:29 – TestAccelerometer]
trouble writing output: shouldn’t happen
[2009-10-13 03:18:29 – TestAccelerometer]Conversion to Dalvik format failed with error 2

[2009-10-13 03:18:29 – TestAccelerometer]
trouble writing output: shouldn’t happen
[2009-10-13 03:18:29 – TestAccelerometer]Conversion to Dalvik format failed with error 2

The error occur when I save my Activity (I have auto-build on, so it could be either the saving or building process).

Like a lazy programmer would do, the first thing I do was trying to find the error on Google. Couple of exact error found but no relation can be found since the root cause is entirely different.

After a few comments & builds tries I found that the problem comes from the following line

size = (size > 10? size = 10: size);

Such a little thing one may miss at 3:20 AM. I’m not sure why Dalvik doesn’t like it but I’m glad I found the solution.

If you are receiving this strange error, first thing I suggest you to do is looking for a strange bit inside your code and pray to your heart content.

Off to bed for today, gotta get up to work at 7 AM 😉

วิธีควบคุมการทำงานของ LED บน Android

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

การใช้งานส่วนนี้จะต้องพึ่ง 2 Class ด้วยกัน คือ

import android.app.Notification;
import android.app.NotificationManager;

การเรียกใช้งานนั้นง่ายมากเลย ขั้นแรกให้เราดึงเอาตัว manager ออกมาก่อน(ตัว NotificationManager เป็น Service ที่รันอยู่แล้วดังนั้นการจะเรียกใช้จึงจะไม่ new นะครับ)
NotificationManager nm = ( NotificationManager ) getSystemService( NOTIFICATION_SERVICE );

จากนั้นก็สร้างตัว Notification จริงๆ พร้อมคอนฟิกตามใจต้องการ
Notification n = new Notification();
n.ledARGB = Color.GREEN; //ฟอร์แมตสีเป็น ARGB นะครับ
n.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL; // บอกให้มันทำอะไร
n.ledOnMS = 1000; // การกระพริบว่าจะให้ติดกี่ ms
n.ledOffMS = 200; // การกระพริบว่าจะให้ดับกี่ ms

หลังจากสร้างเสร็จแล้วก็สั่งให้มันทำงานเลยครับ
nm.notify(NOTIF_ID, n);

แค่นี้เองครับก็เสร็จแล้วง่ายมั้ย ทีนี้ไฟ LED ของคุณก็จะสว่างไสวต่อไปจนชั่วฟ้าดินสลาย 555

ถ้าไม่อยากให้มันเป็นแบบนั้นก็อ่านต่อครับ การจะสั่งให้มันหยุดก็ง้ายง่าย แค่นี้เอง
nm.cancel( NOTIF_ID);

ง่ายเหลือเชื่อใช่มั้ยครับ แต่ถ้าเราจะให้มันโชว์เตือนเหมือนตั้งเวลา การมาสั่งปิดคงไม่สะดวกแน่ ดังนั้นจึงมีการนำ Handler เข้ามาช่วยครับ

เริ่มโดยการประกาศตัว Handler ขึ้นมาไว้ใช้คุมก่อนครับ ส่วน Runnable คือตัว Task ที่เราจะใช้ Handler เรียกครับ ดูจากโค้ดจะเห็นว่าผมสั่งให้มันแคนเซิล Notif อย่างเดียวเลย

private Handler mCleanLedHandler;

private Runnable mClearLED_Task = new Runnable(){
public void run(){
synchronized( MyLEDActivity.this){
nm.cancel( NOTIF_ID);
}
}
};

การจะสั่งใช้งานก็แสนง่ายครับ แค่สร้างตัว Handler ขึ้นมาใหม่ก่อน จากนั้นก็สั่งให้มันทำงานในอีกกี่ ms ข้างหน้า อย่างในตัวอย่างนี่ก็ สามวินาทีครับ

mCleanLedHandler = new Handler();
mCleanLedHandler.postDelayed(mClearLED_Task, 3000 );

นำโค้ดด้านบนนี่ไปต่อจากโค้ดที่ทำให้ LED ติด แค่นี้มันก็จะดับเองภายในเวลาที่กำหนดครับ

ลองนำโค้ดไปศึกษาดูครับ สงสัยตรงไหนโพสต์ถามได้นะครับ

Happy Coding!!

Hello world!

Every programmer starts with “Hello World”, Android is no exception.

How else can one knows if there’s application is working? There is no simpler way of checking but to run a Hello World application to check. If you get the result, then it’s all ready to roll.


English speaker

If you happen to stumble upon this blog but clueless of what I'm saying, feel free to request an English version of the page you'd like information for. I'd gladly posting a new post in English when I'm available.

Blog request

There is no request being work on at the moment.

Category

Android