开发者

Android: Adding .xml resources to an array

UPDATE:

So I tried the AssetManager way and ended up with this:

...
...
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xrp = factory.newPullParser();

AssetManager assmgr = context.getAssets();

xrp.setInput(assmgr.open("levels/level_1.xml"), null);

//Object attributes
String type = null;
int x = 0;
int y = 0;
double angleRads = 0;

int eventType = xrp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
    if (eventType == XmlPullParser.START_DOCUMENT) {
    } else if (eventType == XmlPullParser.START_TAG) {
        if (xrp.getName().equals("Position")) {
            while (!xrp.getName().equals("X")) {  <----NULLPOINTER EXCEPTION HERE
                eventType = xrp.next();
            }           
...
...

Now this code used to work fine when xrp was a XmlResourceParser but now I get this error message:

06-01 05:13:56.797: ERROR/AndroidRun开发者_如何学JAVAtime(946): Caused by: java.lang.NullPointerException
06-01 05:13:56.797: ERROR/AndroidRuntime(946):     at com.stickfigs.blockball.BlockBallView$BlockBallThread.initLevel(BlockBallView.java:342)

I don't understand why this isn't working anymore, I marked the line where the nullpointerexception is happening in the code above with an arrow.

=== vvvOLDvvv ===

In my res/xml/ folder I have a bunch of files called level_#.xml (ex: level_1.xml, level_2.xml, level_21.xml) and I have a spinner widget that I want to populate with the id names of all of the .xml files in this folder that follow the naming convention level_#.xml and only those.

I think I figured out how to set up the widget:

Spinner lsSpinner = (Spinner) findViewById(R.id.levelselect_spinner);

String levels[] = {"level_1","level_2","level_55"};

ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, levels);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
lsSpinner.setAdapter(spinnerArrayAdapter);

Now I just need to generate levels[] dynamically like I explained before...

How do I go about doing this?


The straightforward answer is

String[] levels = new String[LEVELS];
for (int i=0; i< LEVELS; i++) {
    int j = i+1;
    levels[i] = new String ("level_"+j);
}

But I have the feeling that that's not what you want.

If you want a reference to the actual ID of each level_XX.xml, then you will need to do it manually, since each R.layout.levelXX is an actual int and there is no possible way to secure consecutive numbering for them (even if the names you use are consecutive).


Since the previous question has been solved, I think it is better to create another question to let folks help you. But in the name of convenience please allow me to answer you here.

What do you think if xrp.getName() will never equal to "X"? Will xrp goto next until null? I suggest you make a breakpoint here and have a check.

By the way, I saw you are struggling in xml in android, I may suggest you use some ext library which may let life easier. Like this: http://brainflush.wordpress.com/2009/05/19/the-force-unleashed-xmlxpath-on-android-using-dom4j-and-jaxen/

Hope this may help.


Have a look at AssetManager: http://developer.android.com/reference/android/content/res/AssetManager.html#list(java.lang.String)

And then you can new a file and getName() Link: http://developer.android.com/reference/java/io/File.html#getName()

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜