• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Cannot access R.id.ImageView??

Hello,

I just got started with Android. I looked on Google for about twenty minutes but can't find what I'm looking for.

I successfully created a Button in my layout XML and created an onClickListener for that button (using findViewById()). Now, when I try to use the same function to find the ImageView id, it won't find it.

When I enter the R.id and let the auto-complete show suggestions, I see Button01 but not the ImageView01.

I appreciate your time and support, in advance =]
 
you must add an imageView like the button to the xml

I didn't mention it in my first post on accident, but I did put the imageView into the XML. In fact, I can see the ImageView, edit its properties in the Layout view, and see its code in the XML. I simply can't access the id.

Thanks for your guidance thus far.
 
Upvote 0
Hi,

This might seem pretty obvious, but did you give your image view an ID in the XML, and have you checked for typos, or any other errors in the XML? Errors might cause the build to fail, and stop the R class from being regenerated?

Here's an extract from one of my apps:

Code:
  <ImageView 

     android:id="@+id/screenshot"

     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.4"
     android:src="@drawable/screenshot"
     android:gravity="center_horizontal"
  />

And the "screenshot" id appears in my R.java

Code:
public final class R {
    public static final class attr {
    }

    public static final class drawable {
        . . .
    }

    public static final class id {
        . . .
        public static final int screenshot=0x7f060005;
        . . .
    }
  . . .

You could try posting your XML file here to see if anyone can spot any problems.

Regards,

Mark
 
Upvote 0
I'll try to help, but I'm afraid I don't understand your last post.

You're declaring an int variable in Java? Is it a member variable, or just a local variable within a method?

What do you mean by "call the variable in another function"?
Are you trying to access a local variable in one method from another method?

Do you get compilation errors, or just an error when you run your app?

I don't understand how declaring a variable as public would cause an error, unless you're describing a basic Java syntax problem. (This might not be the best forum for Java questions, if it's not an Android specific problem.)

I'd need to see your code to offer any suggestions. It would also be useful to see any error messages that you're getting.

Mark

p.s.
If you post some code please enclose it in the code tags, so layout and indentation are preserved. (The # button in the toolbar). You'd be amazed how many people don't bother, and it makes it really hard to read the code.
 
Upvote 0
I'll try to help, but I'm afraid I don't understand your last post.

You're declaring an int variable in Java? Is it a member variable, or just a local variable within a method?

What do you mean by "call the variable in another function"?
Are you trying to access a local variable in one method from another method?

Do you get compilation errors, or just an error when you run your app?

I don't understand how declaring a variable as public would cause an error, unless you're describing a basic Java syntax problem. (This might not be the best forum for Java questions, if it's not an Android specific problem.)

I'd need to see your code to offer any suggestions. It would also be useful to see any error messages that you're getting.

Mark

p.s.
If you post some code please enclose it in the code tags, so layout and indentation are preserved. (The # button in the toolbar). You'd be amazed how many people don't bother, and it makes it really hard to read the code.

Mark,

My apologies, I haven't used forums much in the past and am still trying to get comfortable with the individual cultures and rules.

I see what you are saying, and I think you're right in judging that this question is in the wrong forum. I will do some Java reading later tonight, and post in the correct forum if I still can't find the answer.

I do appreciate your very helpful nature!
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones