Java Question constant expression required case R.id.bla_bla

Johan Schoeman

Expert
Licensed User
Longtime User
Need some advise on this problem as I have came across it on a number of occasions before. I edit java code with Notepad ++ and compile it with SLC. But whenever there is a switch statements such as...
B4X:
    switch (message.what) {
      case R.id.bla_bla:
...in the java code I get an error constant expression required case R.id.bla_bla

The way to fix it is to change all the switch statements into if/else is/else constructs but that is a real pain in the backside especially if there are numerous switch statements in numerous classes in the project that one wraps.

I am sure there must be a better solution than to go through the switch-to-if exercise? Can someone please give me some pointers to solve this?
 

Johan Schoeman

Expert
Licensed User
Longtime User
A number of the classes in this project makes use of switch statements based on the IDs of resources

https://github.com/zxing/zxing

I am trying to build the latest/most recent version of Zxing
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
note that this project is not just the source. it is the source for several project, a sample app which does have a huge R class and i guess the app is referencing the several layouts used in this app.

Be more specific please. give a congrete path to the java you are talking about
 

Johan Schoeman

Expert
Licensed User
Longtime User

DonManfred

Expert
Licensed User
Longtime User
this is the r file generated with rgenerator-tool

B4X:
import anywheresoftware.b4a.BA;

public class R {
    public static final class xml {
        public static int preferences = BA.applicationContext.getResources().getIdentifier("preferences", "xml", BA.packageName);
    }
    public static final class layout {
        public static int capture = BA.applicationContext.getResources().getIdentifier("capture", "layout", BA.packageName);
        public static int help = BA.applicationContext.getResources().getIdentifier("help", "layout", BA.packageName);
        public static int search_book_contents = BA.applicationContext.getResources().getIdentifier("search_book_contents", "layout", BA.packageName);
        public static int search_book_contents_header = BA.applicationContext.getResources().getIdentifier("search_book_contents_header", "layout", BA.packageName);
        public static int search_book_contents_list_item = BA.applicationContext.getResources().getIdentifier("search_book_contents_list_item", "layout", BA.packageName);
        public static int encode = BA.applicationContext.getResources().getIdentifier("encode", "layout", BA.packageName);
        public static int history_list_item = BA.applicationContext.getResources().getIdentifier("history_list_item", "layout", BA.packageName);
        public static int bookmark_picker_list_item = BA.applicationContext.getResources().getIdentifier("bookmark_picker_list_item", "layout", BA.packageName);
        public static int app_picker_list_item = BA.applicationContext.getResources().getIdentifier("app_picker_list_item", "layout", BA.packageName);
        public static int share = BA.applicationContext.getResources().getIdentifier("share", "layout", BA.packageName);
    }
    public static final class string {
        public static int msg_bulk_mode_scanned = BA.applicationContext.getResources().getIdentifier("msg_bulk_mode_scanned", "string", BA.packageName);
        public static int app_name = BA.applicationContext.getResources().getIdentifier("app_name", "string", BA.packageName);
        public static int msg_camera_framework_bug = BA.applicationContext.getResources().getIdentifier("msg_camera_framework_bug", "string", BA.packageName);
        public static int button_ok = BA.applicationContext.getResources().getIdentifier("button_ok", "string", BA.packageName);
        public static int msg_default_status = BA.applicationContext.getResources().getIdentifier("msg_default_status", "string", BA.packageName);
        public static int msg_error = BA.applicationContext.getResources().getIdentifier("msg_error", "string", BA.packageName);
        public static int msg_invalid_value = BA.applicationContext.getResources().getIdentifier("msg_invalid_value", "string", BA.packageName);
        public static int sbc_name = BA.applicationContext.getResources().getIdentifier("sbc_name", "string", BA.packageName);
        public static int msg_sbc_searching_book = BA.applicationContext.getResources().getIdentifier("msg_sbc_searching_book", "string", BA.packageName);
        public static int msg_sbc_failed = BA.applicationContext.getResources().getIdentifier("msg_sbc_failed", "string", BA.packageName);
        public static int msg_sbc_results = BA.applicationContext.getResources().getIdentifier("msg_sbc_results", "string", BA.packageName);
        public static int msg_sbc_book_not_searchable = BA.applicationContext.getResources().getIdentifier("msg_sbc_book_not_searchable", "string", BA.packageName);
        public static int msg_sbc_no_page_returned = BA.applicationContext.getResources().getIdentifier("msg_sbc_no_page_returned", "string", BA.packageName);
        public static int msg_sbc_page = BA.applicationContext.getResources().getIdentifier("msg_sbc_page", "string", BA.packageName);
        public static int msg_sbc_snippet_unavailable = BA.applicationContext.getResources().getIdentifier("msg_sbc_snippet_unavailable", "string", BA.packageName);
        public static int menu_encode_mecard = BA.applicationContext.getResources().getIdentifier("menu_encode_mecard", "string", BA.packageName);
        public static int menu_encode_vcard = BA.applicationContext.getResources().getIdentifier("menu_encode_vcard", "string", BA.packageName);
        public static int msg_unmount_usb = BA.applicationContext.getResources().getIdentifier("msg_unmount_usb", "string", BA.packageName);
        public static int msg_encode_contents_failed = BA.applicationContext.getResources().getIdentifier("msg_encode_contents_failed", "string", BA.packageName);
        public static int contents_text = BA.applicationContext.getResources().getIdentifier("contents_text", "string", BA.packageName);
        public static int contents_email = BA.applicationContext.getResources().getIdentifier("contents_email", "string", BA.packageName);
        public static int contents_phone = BA.applicationContext.getResources().getIdentifier("contents_phone", "string", BA.packageName);
        public static int contents_sms = BA.applicationContext.getResources().getIdentifier("contents_sms", "string", BA.packageName);
        public static int contents_contact = BA.applicationContext.getResources().getIdentifier("contents_contact", "string", BA.packageName);
        public static int contents_location = BA.applicationContext.getResources().getIdentifier("contents_location", "string", BA.packageName);
        public static int history_clear_one_history_text = BA.applicationContext.getResources().getIdentifier("history_clear_one_history_text", "string", BA.packageName);
        public static int history_email_title = BA.applicationContext.getResources().getIdentifier("history_email_title", "string", BA.packageName);
        public static int msg_sure = BA.applicationContext.getResources().getIdentifier("msg_sure", "string", BA.packageName);
        public static int button_cancel = BA.applicationContext.getResources().getIdentifier("button_cancel", "string", BA.packageName);
        public static int history_empty = BA.applicationContext.getResources().getIdentifier("history_empty", "string", BA.packageName);
        public static int history_empty_detail = BA.applicationContext.getResources().getIdentifier("history_empty_detail", "string", BA.packageName);
        public static int button_add_contact = BA.applicationContext.getResources().getIdentifier("button_add_contact", "string", BA.packageName);
        public static int button_show_map = BA.applicationContext.getResources().getIdentifier("button_show_map", "string", BA.packageName);
        public static int button_dial = BA.applicationContext.getResources().getIdentifier("button_dial", "string", BA.packageName);
        public static int button_email = BA.applicationContext.getResources().getIdentifier("button_email", "string", BA.packageName);
        public static int result_address_book = BA.applicationContext.getResources().getIdentifier("result_address_book", "string", BA.packageName);
        public static int button_add_calendar = BA.applicationContext.getResources().getIdentifier("button_add_calendar", "string", BA.packageName);
        public static int result_calendar = BA.applicationContext.getResources().getIdentifier("result_calendar", "string", BA.packageName);
        public static int result_email_address = BA.applicationContext.getResources().getIdentifier("result_email_address", "string", BA.packageName);
        public static int button_get_directions = BA.applicationContext.getResources().getIdentifier("button_get_directions", "string", BA.packageName);
        public static int result_geo = BA.applicationContext.getResources().getIdentifier("result_geo", "string", BA.packageName);
        public static int button_product_search = BA.applicationContext.getResources().getIdentifier("button_product_search", "string", BA.packageName);
        public static int button_book_search = BA.applicationContext.getResources().getIdentifier("button_book_search", "string", BA.packageName);
        public static int button_search_book_contents = BA.applicationContext.getResources().getIdentifier("button_search_book_contents", "string", BA.packageName);
        public static int button_custom_product_search = BA.applicationContext.getResources().getIdentifier("button_custom_product_search", "string", BA.packageName);
        public static int result_isbn = BA.applicationContext.getResources().getIdentifier("result_isbn", "string", BA.packageName);
        public static int button_web_search = BA.applicationContext.getResources().getIdentifier("button_web_search", "string", BA.packageName);
        public static int result_product = BA.applicationContext.getResources().getIdentifier("result_product", "string", BA.packageName);
        public static int msg_default_mms_subject = BA.applicationContext.getResources().getIdentifier("msg_default_mms_subject", "string", BA.packageName);
        public static int msg_intent_failed = BA.applicationContext.getResources().getIdentifier("msg_intent_failed", "string", BA.packageName);
        public static int button_sms = BA.applicationContext.getResources().getIdentifier("button_sms", "string", BA.packageName);
        public static int button_mms = BA.applicationContext.getResources().getIdentifier("button_mms", "string", BA.packageName);
        public static int result_sms = BA.applicationContext.getResources().getIdentifier("result_sms", "string", BA.packageName);
        public static int result_tel = BA.applicationContext.getResources().getIdentifier("result_tel", "string", BA.packageName);
        public static int button_share_by_email = BA.applicationContext.getResources().getIdentifier("button_share_by_email", "string", BA.packageName);
        public static int button_share_by_sms = BA.applicationContext.getResources().getIdentifier("button_share_by_sms", "string", BA.packageName);
        public static int result_text = BA.applicationContext.getResources().getIdentifier("result_text", "string", BA.packageName);
        public static int button_open_browser = BA.applicationContext.getResources().getIdentifier("button_open_browser", "string", BA.packageName);
        public static int result_uri = BA.applicationContext.getResources().getIdentifier("result_uri", "string", BA.packageName);
        public static int button_wifi = BA.applicationContext.getResources().getIdentifier("button_wifi", "string", BA.packageName);
        public static int wifi_changing_network = BA.applicationContext.getResources().getIdentifier("wifi_changing_network", "string", BA.packageName);
        public static int result_wifi = BA.applicationContext.getResources().getIdentifier("result_wifi", "string", BA.packageName);
        public static int msg_google_books = BA.applicationContext.getResources().getIdentifier("msg_google_books", "string", BA.packageName);
        public static int msg_google_product = BA.applicationContext.getResources().getIdentifier("msg_google_product", "string", BA.packageName);
        public static int msg_redirect = BA.applicationContext.getResources().getIdentifier("msg_redirect", "string", BA.packageName);
    }
    public static final class color {
        public static int result_points = BA.applicationContext.getResources().getIdentifier("result_points", "color", BA.packageName);
        public static int viewfinder_mask = BA.applicationContext.getResources().getIdentifier("viewfinder_mask", "color", BA.packageName);
        public static int result_view = BA.applicationContext.getResources().getIdentifier("result_view", "color", BA.packageName);
        public static int viewfinder_laser = BA.applicationContext.getResources().getIdentifier("viewfinder_laser", "color", BA.packageName);
        public static int possible_result_points = BA.applicationContext.getResources().getIdentifier("possible_result_points", "color", BA.packageName);
    }
    public static final class menu {
        public static int capture = BA.applicationContext.getResources().getIdentifier("capture", "menu", BA.packageName);
        public static int encode = BA.applicationContext.getResources().getIdentifier("encode", "menu", BA.packageName);
        public static int history = BA.applicationContext.getResources().getIdentifier("history", "menu", BA.packageName);
    }
    public static final class raw {
        public static int beep = BA.applicationContext.getResources().getIdentifier("beep", "raw", BA.packageName);
    }
    public static final class drawable {
        public static int launcher_icon = BA.applicationContext.getResources().getIdentifier("launcher_icon", "drawable", BA.packageName);
    }
    public static final class BEETAGG {
        public static int COM = BA.applicationContext.getResources().getIdentifier("COM", "BEETAGG", BA.packageName);
    }
    public static final class id {
        public static int viewfinder_view = BA.applicationContext.getResources().getIdentifier("viewfinder_view", "id", BA.packageName);
        public static int result_view = BA.applicationContext.getResources().getIdentifier("result_view", "id", BA.packageName);
        public static int status_view = BA.applicationContext.getResources().getIdentifier("status_view", "id", BA.packageName);
        public static int preview_view = BA.applicationContext.getResources().getIdentifier("preview_view", "id", BA.packageName);
        public static int menu_share = BA.applicationContext.getResources().getIdentifier("menu_share", "id", BA.packageName);
        public static int menu_history = BA.applicationContext.getResources().getIdentifier("menu_history", "id", BA.packageName);
        public static int menu_settings = BA.applicationContext.getResources().getIdentifier("menu_settings", "id", BA.packageName);
        public static int menu_help = BA.applicationContext.getResources().getIdentifier("menu_help", "id", BA.packageName);
        public static int decode_succeeded = BA.applicationContext.getResources().getIdentifier("decode_succeeded", "id", BA.packageName);
        public static int barcode_image_view = BA.applicationContext.getResources().getIdentifier("barcode_image_view", "id", BA.packageName);
        public static int format_text_view = BA.applicationContext.getResources().getIdentifier("format_text_view", "id", BA.packageName);
        public static int type_text_view = BA.applicationContext.getResources().getIdentifier("type_text_view", "id", BA.packageName);
        public static int time_text_view = BA.applicationContext.getResources().getIdentifier("time_text_view", "id", BA.packageName);
        public static int meta_text_view = BA.applicationContext.getResources().getIdentifier("meta_text_view", "id", BA.packageName);
        public static int meta_text_view_label = BA.applicationContext.getResources().getIdentifier("meta_text_view_label", "id", BA.packageName);
        public static int contents_text_view = BA.applicationContext.getResources().getIdentifier("contents_text_view", "id", BA.packageName);
        public static int contents_supplement_text_view = BA.applicationContext.getResources().getIdentifier("contents_supplement_text_view", "id", BA.packageName);
        public static int result_button_view = BA.applicationContext.getResources().getIdentifier("result_button_view", "id", BA.packageName);
        public static int return_scan_result = BA.applicationContext.getResources().getIdentifier("return_scan_result", "id", BA.packageName);
        public static int launch_product_query = BA.applicationContext.getResources().getIdentifier("launch_product_query", "id", BA.packageName);
        public static int restart_preview = BA.applicationContext.getResources().getIdentifier("restart_preview", "id", BA.packageName);
        public static int decode_failed = BA.applicationContext.getResources().getIdentifier("decode_failed", "id", BA.packageName);
        public static int decode = BA.applicationContext.getResources().getIdentifier("decode", "id", BA.packageName);
        public static int quit = BA.applicationContext.getResources().getIdentifier("quit", "id", BA.packageName);
        public static int help_contents = BA.applicationContext.getResources().getIdentifier("help_contents", "id", BA.packageName);
        public static int query_text_view = BA.applicationContext.getResources().getIdentifier("query_text_view", "id", BA.packageName);
        public static int query_button = BA.applicationContext.getResources().getIdentifier("query_button", "id", BA.packageName);
        public static int result_list_view = BA.applicationContext.getResources().getIdentifier("result_list_view", "id", BA.packageName);
        public static int page_number_view = BA.applicationContext.getResources().getIdentifier("page_number_view", "id", BA.packageName);
        public static int snippet_view = BA.applicationContext.getResources().getIdentifier("snippet_view", "id", BA.packageName);
        public static int menu_encode = BA.applicationContext.getResources().getIdentifier("menu_encode", "id", BA.packageName);
        public static int image_view = BA.applicationContext.getResources().getIdentifier("image_view", "id", BA.packageName);
        public static int menu_history_send = BA.applicationContext.getResources().getIdentifier("menu_history_send", "id", BA.packageName);
        public static int menu_history_clear_text = BA.applicationContext.getResources().getIdentifier("menu_history_clear_text", "id", BA.packageName);
        public static int history_title = BA.applicationContext.getResources().getIdentifier("history_title", "id", BA.packageName);
        public static int history_detail = BA.applicationContext.getResources().getIdentifier("history_detail", "id", BA.packageName);
        public static int bookmark_title = BA.applicationContext.getResources().getIdentifier("bookmark_title", "id", BA.packageName);
        public static int bookmark_url = BA.applicationContext.getResources().getIdentifier("bookmark_url", "id", BA.packageName);
        public static int app_picker_list_item_label = BA.applicationContext.getResources().getIdentifier("app_picker_list_item_label", "id", BA.packageName);
        public static int app_picker_list_item_icon = BA.applicationContext.getResources().getIdentifier("app_picker_list_item_icon", "id", BA.packageName);
        public static int share_contact_button = BA.applicationContext.getResources().getIdentifier("share_contact_button", "id", BA.packageName);
        public static int share_bookmark_button = BA.applicationContext.getResources().getIdentifier("share_bookmark_button", "id", BA.packageName);
        public static int share_app_button = BA.applicationContext.getResources().getIdentifier("share_app_button", "id", BA.packageName);
        public static int share_clipboard_button = BA.applicationContext.getResources().getIdentifier("share_clipboard_button", "id", BA.packageName);
        public static int share_text_view = BA.applicationContext.getResources().getIdentifier("share_text_view", "id", BA.packageName);
    }
}

I guess you´ll find a lot of matches if you compare the line you told with this r-class content....
 

Johan Schoeman

Expert
Licensed User
Longtime User
this is the code from the android app provided. SURE the use R class references. Why you want to compile the sampleapp? I guess you should compile it from android Studio...

The code is not part of the core
I am compiling it all (core + app) into a single jar. Have now converted all the switch statements into if/else if/else statements and it compiled fine. There were not too many other classes that required the conversion. Thanks for looking into it.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Yes, I have also generated it but for some unknown reason the switch statements don't work if they refer to a resource (case R.id.xxx)
 

Johan Schoeman

Expert
Licensed User
Longtime User
I have already converted all the switch statements into if/else if/ else (as what I have done before with other projects) and it compiled fine. I was just hoping that there was an easier solution. I managed to fire up the project from within B4A....

 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…