10 Replies - 2365 Views - Last Post: 30 June 2011 - 01:45 PM

#1 phil1000  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 176
  • Joined: 26-February 09

Issues with R.java and xml

Posted 23 June 2011 - 06:46 PM

So just as I was getting the hang of things, I come to a complete stop because of this small issue. So I understand you have XML code then you can reference through R.java in your actual code for things like menus and the like. Well here's the code I added in:

The XML:
<item android:id="@+id/itemPrefs" 
        android:title="@string/titlePrefs" 
        android:icon="@android:drawable/ic_menu_preferences"></item>
    <!-- added the bottom two -->
    <item android:title="@string/titleServiceStart" 
        android:id="@+id/itemServiceStart" 
        android:icon="@android:drawable/ic_media_play"></item>
    <item android:title="@string/titleServiceStop" 
        android:id="@+id/itemServiceStop" 
        android:icon="@android:drawable/ic_media_pause"></item> 



The Java code
            case R.id.itemServiceStart:
                startService(new Intent(this, UpdaterService.class));
                break;
            case R.id.itemServiceStop:
                stopService(new Intent(this, UpdaterService.class));
                break;
//top two are new
            case R.id.prefs:
                Intent i = new Intent(this, PrefsActivity.class);
                startActivity(i);
                break;



I made note of what is new because before they were put in, all the code worked, and now I'm getting cannot find symbol errors and it's pissing me off. I've done clean builds and check R.java and it all checks out the variables are being made. Help?

Is This A Good Question/Topic? 0
  • +

Replies To: Issues with R.java and xml

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: Issues with R.java and xml

Posted 24 June 2011 - 06:50 AM

Try resetting Eclipse and a clean build.

Also this code:
case R.id.prefs:



Should be:
case R.id.itemPrefs



since that is what you called it in your XML. Let me know how it goes. If it doesn't work, let me know and we'll try something else.
Was This Post Helpful? 0
  • +
  • -

#3 Apokio  Icon User is offline

  • D.I.C Addict

Reputation: 134
  • Posts: 509
  • Joined: 14-August 09

Re: Issues with R.java and xml

Posted 24 June 2011 - 12:15 PM

I had a problem like this one before and I can't see your onCreate method so i don't know if you are havin the same problem. But, I had to declare the new instances of the items in the onCreate method. If I tried to to declare the variable and the new instance all before the onCreate method it would crash everytime.
Was This Post Helpful? 0
  • +
  • -

#4 phil1000  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 176
  • Joined: 26-February 09

Re: Issues with R.java and xml

Posted 24 June 2011 - 06:58 PM

I renamed it and it didn't work. :sad3:

well here's my onCreate method:
public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.status);
        

        editText = (EditText)findViewById(R.id.editText);
        updateButton = (Button)findViewById(R.id.buttonUpdate);
        updateButton.setonclickListener(this);
        
        textCount = (TextView)findViewById(R.id.textCount);
        textCount.setText(Integer.toString(140));
        textCount.setTextColor(Color.GREEN);
        
        editText.addTextChangedListener(this);
    }



it's aggravating, because I'll even comment out what I had added, and it still fails.

This post has been edited by phil1000: 24 June 2011 - 07:00 PM

Was This Post Helpful? 0
  • +
  • -

#5 phil1000  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 176
  • Joined: 26-February 09

Re: Issues with R.java and xml

Posted 26 June 2011 - 07:55 AM

So I've been deleting xmls to see if things would work, and it isn't. I'm starting to really get frustrated with this crap. I'm really disappointed in Google in such a poor design scheme.
Was This Post Helpful? 0
  • +
  • -

#6 Apokio  Icon User is offline

  • D.I.C Addict

Reputation: 134
  • Posts: 509
  • Joined: 14-August 09

Re: Issues with R.java and xml

Posted 29 June 2011 - 11:06 AM

In your findViewById do you have an EditText with the id of editText in the xml file? In the xml you have posted you have them named itemServiceStart, and itemServiceStop. If you post your entire xml, java, and R file I might be able to get a better picture. By if you are trying to reference something that doesn't exist in the R file using findViewById it will throw that error.
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: Issues with R.java and xml

Posted 29 June 2011 - 05:45 PM

Apokio is right. You have to reference the proper name for the id. For example, let's look at just this XML:
<item android:id="@+id/itemPrefs"
	        android:title="@string/titlePrefs"
	        android:icon="@android:drawable/ic_menu_preferences"></item>



What the android:id attribute does is gives the tag a unique id. The @+id/ part of the value tells android that it needs to create a new id with the name that follows - in this case, "itemPrefs". When the Android XML compiler adds a reference to R.java, the name will be the same as you specified here. So to reference that ID, you will need this:
R.id.itemPrefs in your findViewById() method.
Was This Post Helpful? 0
  • +
  • -

#8 phil1000  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 176
  • Joined: 26-February 09

Re: Issues with R.java and xml

Posted 29 June 2011 - 06:12 PM

So I've been trying to quarantine the issue, and I think it's an issue with my StatusActivity.java because other files have similar function calls but not receiving an error. Here's something that I hope will clarify the issue quickly.

The StatusActivity.java:
setContentView(R.layout.status);

The R.java
    public static final class layout {
        public static final int status=0x7f030000;
    }


And I have a folder named layout with an xml file named status. And that's the case with all other calls to R.java in StatusActivity. This was a working statement until the issue flared up. If it makes things easier here's status.xml:
<?xml version="1.0" encoding="UTF-8"?> 
<!-- Main Layout of Status Activity -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background">

<!--Title TextView -->
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="30sp"
    android:layout_margin="10dp"
    android:text="@string/titleStatus"
    android:textColor="@android:color/white"
    />

<!-- Text Counter TextView -->
<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:id="@+id/textCount"
    android:text="000"
    android:layout_marginRight="10dp"
    />
<!-- Status EditText -->
<EditText 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:hint="@string/hintText"
    android:id="@+id/editText"
    android:gravity="top|center_horizontal"
    android:background="#cfff"></EditText>
<Button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/buttonUpdate"
    android:textSize="20sp"
    android:id="@+id/buttonUpdate"></Button>
</LinearLayout>


This post has been edited by phil1000: 29 June 2011 - 06:14 PM

Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: Issues with R.java and xml

Posted 29 June 2011 - 06:31 PM

I don't see any ID in your XML with the name "status". That's your issue. Delete R.java and clean the build. It should regenerate R.java with the correct id names. Also, that XML file is in /res/layout/ right?
Was This Post Helpful? 0
  • +
  • -

#10 phil1000  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 176
  • Joined: 26-February 09

Re: Issues with R.java and xml

Posted 30 June 2011 - 01:31 PM

Well it doesn't need an id because I'm loading an xml as a view. Also if I copy and paste that line into another file, it complies, so it 's quarantined to that specific file. Gah, how frustrating. And any and all calls to R.java in StatusActivity fail.

And I added the line in R.java that states that it's in there and should be working.

This post has been edited by phil1000: 30 June 2011 - 01:31 PM

Was This Post Helpful? 0
  • +
  • -

#11 phil1000  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 176
  • Joined: 26-February 09

Re: Issues with R.java and xml

Posted 30 June 2011 - 01:45 PM

Well good news it works! I don't know what I did, so I'm sorry that I can't solve it for others with this issue, but I don't think it's that common of a problem.

Thank you to all who helped!

This post has been edited by phil1000: 30 June 2011 - 01:48 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1