16 Mar 2011, 00:11
Generic-user-small

Wil Haywood (3 posts)

Hi all,

I have looked over this code endlessly. There are numerous suggestions here that i have employed, including a ‘clean’.. but I cannot get past the variable issue. The code worked wonderfully until I constructed the menu. Any thoughts?

16 Mar 2011, 04:44
Generic-user-small

Wil Haywood (3 posts)

Suduko.java

package org.example.sudoku;

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class Sudoku extends Activity implements OnClickListener{ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

//Set up listeners for all the buttons
       View continueButton = findViewById(R.id.continue_button);
       continueButton.setOnClickListener(this);
       View newButton = findViewById(R.id.new_button);
       newButton.setOnClickListener(this);
       View aboutButton = findViewById(R.id.about_button);
       aboutButton.setOnClickListener(this);
       View exitButton = findViewById(R.id.exit_button);
       exitButton.setOnClickListener(this);
   }
// getMenuInflater( ) returns an instance of MenuInflater that we use to read
// the menu definition from XML and turns it into a real view. When
// the user selects any menu item, onOptionsItemSelected( ) will be called.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.settings:
        startActivity(new Intent(this, Prefs.class));
        return true;
        // More items go here (if any) ...
    }
    return false;
   } 
}
16 Mar 2011, 14:50
Generic-user-small

Anthony Shaw (115 posts)

It sounds like the resources are not being built, so maybe there is a problem in res\layout\main.xml or res\layout-land\main.xml or res\xml\settings.xml. Open each one in the Eclipse editor and see if there are any warning messages in them.

17 Mar 2011, 01:20
Burnette_ed_pragsmall

Ed Burnette (1316 posts)

Also look in the Problems and Console views in Eclipse. Usually an “R cannot be resolved” error is caused by a typo in the XML.

Here’s some email I sent another reader today who was having the same problem. It shows the diagnostic process you need to go through to find the error:

“[After loading the code in Eclipse] I noticed this error in the Problems view:

Unparsed aapt error(s)! Check the console for output.    Sudoku        line 1    Android ADT Problem

That told me to look at the Console view where I found errors like this:

[2011-03-16 19:43:09 - Sudoku] W/ResourceType(  768): Bad XML block: header size 150 or total size 0 is larger than data size 0
[2011-03-16 19:43:09 - Sudoku] C:\Development\workspace\Sudoku\res\xml\settings.xml:3: error: Error: No resource found that matches the given name (at 'title' with value '@string/music_title').

That told me there must be something wrong in the string definitions and to look at the res/values/strings.xml file, where I found a syntax error. This section:

    <string name="about_text">
    <string name="settings_label">Settings...</string>
    <string name="settings_title">Sudoku settings</string>

       \
    Sudoku is a logic-based number placement puzzle.

has the starting string element for about_text in the wrong place. It should be moved down like this:

    <string name="settings_label">Settings...</string>
    <string name="settings_title">Sudoku settings</string>

    <string name="about_text">\
    Sudoku is a logic-based number placement puzzle.

In XML, every starting element like “<string>” must be matched by an ending element like “</string>”. Once I fixed that the errors about R not being resolvable went away.”

20 Mar 2011, 18:14
Generic-user-small

Wil Haywood (3 posts)

Thanks, Ed..

There was something out of place on another string element. Great suggestions…

21 Mar 2011, 00:00
Wildcatbig_pragsmall

Pfeffa-rah (1 post)

Hi there,

I ran into the same problem.
I get the “R cannot be resolved to a variable” error in all my .java files…

All strings seem to be allright. They are all there and all tags are nicely closed etc.

The console just tells me this:
[2011-03-21 00:38:03 – Sudoku] W/ResourceType( 6636): Bad XML block: no root element node found
[2011-03-21 00:38:03 – Sudoku] C:\eclipse\workspace\Sudoku\res\menu\menu.xml:5: error: Error parsing XML: not well-formed (invalid token)

(the W/ResourceType number changes…)

I checked my menu.xml several times, and since there are only a few lines there, its was easy enough.
In the end I just pasted the code from the code I got from this website underneath the code I typed.
There were NO differences that I could find.
However I deleted the code I typed and saved.

...

The errors were gone and left me completely baffled….

Any ideas what I went wrong?
(I really must believe that I can compare and check 6 lines of code, or I should just quit right now!)

greetz,
Pfeffa-rah

21 Mar 2011, 19:41
Generic-user-small

Mike Puckett (1 post)

I had the same problems. Basically, R.java wasn’t getting populated. I found that, in order to fix it, I had to move the to the end. Perhaps, because the about_text is so long, XML was having trouble parsing the rest of the values.xml file correctly?

22 Mar 2011, 01:05
Burnette_ed_pragsmall

Ed Burnette (1316 posts)

Some typos are very hard to see, especially when you’re not 100% confident that they’re really there. Changing a colon (:) to a period (.) is only a matter of a couple of pixels on the screen, for example. And just to keep things interesting there’s always the remote possibility that somehow an invisible unprintable character snuck in to spoil your day. (Show whitespace can sometimes help with those, as can 3rd party editors with hex modes).

Matching up opening and closing tags in both XML and Java always gets me. Luckily the Format command (Ctrl+Shift+F in most editors) helps with that particular problem.

  You must be logged in to comment