Cannot access R.id.ImageView??


Last Updated:

  1. Inkybro

    Inkybro Member This Topic's Starter

    Joined:
    Mar 6, 2010
    Messages:
    8
    Likes Received:
    0
    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 =]
     

    Advertisement
  2. honey179190

    honey179190 New Member

    Joined:
    Mar 8, 2010
    Messages:
    1
    Likes Received:
    0
    you must add an imageView like the button to the xml
     
  3. Inkybro

    Inkybro Member This Topic's Starter

    Joined:
    Mar 6, 2010
    Messages:
    8
    Likes Received:
    0
    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.
     
  4. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    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 (Text):
    1.    
    2.   <ImageView
    3.  
    4.      android:id="@+id/screenshot"
    5.  
    6.      android:layout_width="fill_parent"
    7.      android:layout_height="wrap_content"
    8.      android:layout_weight="0.4"
    9.      android:src="@drawable/screenshot"
    10.      android:gravity="center_horizontal"
    11.   />
    12.  
    And the "screenshot" id appears in my R.java

    Code (Text):
    1.  
    2. public final class R {
    3.     public static final class attr {
    4.     }
    5.  
    6.     public static final class drawable {
    7.         . . .
    8.     }
    9.  
    10.     public static final class id {
    11.         . . .
    12.         public static final int screenshot=0x7f060005;
    13.         . . .
    14.     }
    15.   . . .
    16.  
    You could try posting your XML file here to see if anyone can spot any problems.

    Regards,

    Mark
     
  5. Inkybro

    Inkybro Member This Topic's Starter

    Joined:
    Mar 6, 2010
    Messages:
    8
    Likes Received:
    0
    Okay, now I have the ImageView working. Thanks!

    But now, how can I create an Int variable? I tried int number = 0; which works but I need to call the variable in another function, therefore I need it to be public. When I try to make it public, though, it errors on run.

    Any ideas?
     
  6. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    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.
     
  7. Inkybro

    Inkybro Member This Topic's Starter

    Joined:
    Mar 6, 2010
    Messages:
    8
    Likes Received:
    0
    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!
     
  8. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    I'm happy to help. (Well, I'll try) So if you need to ask, please do.
    As a general rule though, you'll get a better response from a java specific forum. Also, your question, and the answers you get, will be more helpful to others who might be having similar problems.

    Mark
     

Share This Page

Loading...