Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

android - Resources$NotFoundException in Graphical Layout ADT preview (but app actually Works)

My problem is that loading an array of strings defined in XML works in the app but will result in an error in the ADT Graphical Layout preview.

Now I can't see any graphics in the Graphical Layout because of this error, and it's difficult to work with other graphics. But the view is loading and displaying the strings fine if I build and run my app.

So I suppose my code is correct but either:

  • I am missing some limitations of the Graphical Layout preview and some workaround
  • or perhaps I'm missing something obvious and doing things wrong even if it seems to work in the app

I have a custom view where I get an array defined by me in an array.xml file.

public class ScoreTable extends View {
  [...]
  @Override
  protected void onDraw(Canvas canvas) {
    [...]
    int score_vals[] = getResources().getIntArray(R.array.score_vals);
    [...]
  }
  [...]
}

My array is defined in res/values/array.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="score_vals">
        <item >10</item>
        <item >20</item>
        <item >50</item>
    </array>
</resources>

Graphical Layout is blank and says:

Int array resource ID #0x7f050000
Exception details are logged in Window > Show View > Error Log

But of course I have "public static final int score_vals=0x7f050000;" in R.java!

The details of this error are in a 50-deep stack, but resumes to this:

android.content.res.Resources$NotFoundException: Int array resource ID #0x7f050000
    at android.content.res.Resources.getIntArray(Resources.java:405)
    at com.threecats.poker.ScoreTable.onDraw(ScoreTable.java:53)
    at android.view.View.draw(View.java:6740)
[...]

So, should getResources().getXXXArray() work in the context of a ADT Graphical Layout preview?

I would like to mention that I tried with both "array" and "array-integer" in the XML, and both work in the app but not in the preview. Also I tried to save the Context from the constructor of the view in a private Context member... didn't help either.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your code is alright but unfortunately there are still some bugs in ADT plugin and there is one of them. Layout Editor has troubles with rendering custom views. I had the same issue and the only workout I have found is checking View.isInEditMode and initializing int array in some other way but not from resources. So your code will look like this:

int score_vals[];
if (isInEditMode()) {
    score_vals = { 10, 20, 50 };
} else {
    score_vals = getResources().getIntArray(R.array.score_vals);
}

And by the way don't create or load any resources in your onDraw methods. I suppose getResources().getIntArray uses some sort of caching but anyway your perfomance may suffer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...