R.java เป็นไฟล์ที่ถูกสร้างขึ้นอัตโนมัติ เพื่อใช้เป็นดัชนีในการอ้างถึงทรัพยากรต่างๆ ใน app ของเรา
A project's
R.java file is an index into all the resources defined in the file. You use this class in your source code as a sort of short-hand way to refer to resources you've included in your project. ถ้าเปิด R.java ดู จะพบค่าดังนี้
public final class R { public static final class attr { } public static final class drawable { public static final int icon=0x7f020000; } public static final class id { public static final int textview=0x7f050000; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; } }
วิธีแก้ปัญหาถ้าเจอ ERROR: unable to open class file R.java
- ลองไปที่ Project -> Clean แล้วก็ Build project -> Run โดยใช้ emulator ที่ตรงกับ project เช่น Google API 8
- ถ้าสร้าง layout เอง ก็ให้ตรวจว่า xml syntax error มั้ย
- ลอง clean & buile หลายรอบแล้ว ก็ยังไม่เกิด gen/R.java สักที ก็เลยสร้าง foler gen ขึ้นมาเอง (โดยคลิกขวาที่โปรเจกต์ -> new fodler) แล้วลอง clean -> build จะเกิด gen/R.java ขึ้น ถ้ายังไม่ได้ก็ลอง clean & build ดูอีกรอบ เรื่องนี้อาจจะเป็นเพราะ Windows 7 ระวังเรื่องความปลอดภัยในการเขียนไฟล์มากเกินไปก็เป็นได้
- บางทีก็ต้องแกล้ง copy มาทั้ง folder gen เลยก็ได้ ไม่รู้เหมือนกันว่าถูกต้องตามหลักวิชามั้ย แต่ไม่ได้ด้วยเล่ห์ก็ต้องเอาด้วยกล เหอๆ
- บางทีก็ต้องแกล้ง copy มาทั้ง folder gen เลยก็ได้ ไม่รู้เหมือนกันว่าถูกต้องตามหลักวิชามั้ย แต่ไม่ได้ด้วยเล่ห์ก็ต้องเอาด้วยกล เหอๆ

ไม่มีความคิดเห็น:
แสดงความคิดเห็น