Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

illegalArgumentException: The key must be an application-specific resource id #25

Closed
mariusk opened this issue Sep 12, 2015 · 10 comments
Closed

Comments

@mariusk
Copy link

mariusk commented Sep 12, 2015

After introducing a few more view to my app using anvil, it starts throwing these when I switch views calling setContentView:

09-12 21:15:13.137  19964-19964/net.kjeldahl.secdrones E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: net.kjeldahl.secdrones, PID: 19964
    java.lang.IllegalArgumentException: The key must be an application-specific resource id.
            at android.view.View.setTag(View.java:18542)
            at trikita.anvil.BaseAttrs$9.apply(BaseAttrs.java:308)
            at trikita.anvil.Anvil.inflateNode(Anvil.java:249)
            at trikita.anvil.Anvil.inflateNode(Anvil.java:233)
            at trikita.anvil.Anvil.inflateNode(Anvil.java:233)
            at trikita.anvil.Anvil.inflateNode(Anvil.java:233)
            at trikita.anvil.Anvil.render(Anvil.java:106)
            at trikita.anvil.RenderableView.onMeasure(RenderableView.java:84)
            at android.view.View.measure(View.java:18788)

Any ideas why, and/or what I need to do to fix it?

@mariusk
Copy link
Author

mariusk commented Sep 12, 2015

So this may very well be related to the fact that I am calling setContentView multiple times from within the same activity. Which leads to the natural question: What IS the proper way to switch view within a running activity?

@zserge
Copy link
Collaborator

zserge commented Sep 12, 2015

I don't think so. I would suggest it's a limitation of setTag(id, value) - try using IDs over 0x80000000, otherwise android treats this as an invalid ID and throws an exception.

@mariusk
Copy link
Author

mariusk commented Sep 12, 2015

Well, I'm not using ids (like none of the examples do). So am I supposed to call setId in my own views, or is it something else you are thinking about?

@zserge
Copy link
Collaborator

zserge commented Sep 12, 2015

Sorry, I'm not talking about view ids. What kind of tag attribute setter do you use? tag(value) or tag(id, value)? If the second one - try higher ID values for the tag.

@mariusk
Copy link
Author

mariusk commented Sep 12, 2015

I'm not setting anything explicitely, which is why I'm confused. I am using Kotlin with the view helpers if that's relevant. For the heck of it, I tried calling setId in my views. Negative view id's don't seem to work (-1, -2 which would equivalent to 0x80000001 etc), but unique values like 0x10000001 etc does seem to work.

@mariusk
Copy link
Author

mariusk commented Sep 12, 2015

So here's one of the views I'm testing with:

class StartupView(c: Context) : RenderableView(c) {
    companion object {
        val TAG = "StartupView"
    }

    override fun view() : Nodes.ViewNode {
        setId(0x10000001)
        Log.i(TAG, "::view")
        val params = LinearLayout.LayoutParams(WRAP, WRAP)
        params.gravity = Gravity.CENTER
        return v<LinearLayout> {
            -size(FILL, FILL)
            -orientation(LinearLayout.VERTICAL)
            -gravity(CENTER)
            -backgroundColor(Color.parseColor("#ffe0e0"))
            //-layoutParams(params)
            v<LinearLayout> {
                -orientation(LinearLayout.HORIZONTAL)
                -gravity(CENTER_HORIZONTAL)
                -size(FILL, WRAP)
                -backgroundColor(Color.parseColor("#e0ffe0"))
                v<TextView> {
                    -text("Initializing..")
                }
            }
        }
    }
}

Yes, it has some inactive code (params etc) which shouldn't affect anything. I also tested addContentView and setting visibility and bringToFront() etc, and that also seems to work, but is a lot more work (which may pay off eventually, I know).

@mariusk
Copy link
Author

mariusk commented Sep 12, 2015

This code from SimpleAttrNode.java looks a bit fishy:

    @Override
    public int hashCode() {
        return value == null ? value.hashCode() : 0;
    }

I would assume null should return zero, and hashCode otherwise. But I'm just guessing.

@zserge
Copy link
Collaborator

zserge commented Dec 22, 2015

Ok, Anvil 0.1.x doesn't use setTag with IDs anymore, and most of the issues referred to in this thread are gone (like broken hashCode implementation), so I'll probably close this issue.

@zserge zserge closed this as completed Dec 22, 2015
@droidfreak8931
Copy link

Hi @mariusk : you may very well use setTag with IDS , but make sure you define these ids in strings.xml file
EX: view.setTag(R.string.yourTag,yourvalue);

@zserge
Copy link
Collaborator

zserge commented Jul 26, 2016

@mariusk @droidfreak8931 Mostly true, except for IDs should rather be declared in ids.xml instead - http://stackoverflow.com/questions/10093975/android-how-to-create-xmls-id then you would be able to do view.setTag(R.id.your_tag, yourValue);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants