Why is this call stack so weird?

2019-09-17 16:24发布

Today I was debugging my android app and it crashes. Here is the call stack:

android.content.res.Resources$NotFoundException: String resource ID #0x8822
        at android.content.res.Resources.getText(Resources.java:246)
        at android.widget.TextView.setText(TextView.java:3860)
        at com.whackanandroid.GameActivity.gameOver(GameActivity.java:68)
        at com.whackanandroid.Game$1.onCountDownFinished(Game.java:79)
        at com.whackanandroid.CountDown$1.run(CountDown.java:23)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:5225)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
        at dalvik.system.NativeStart.main(Native Method)

Then I clicked on the line number links (TextView.java:3860) and it took me to a line of javadoc comment. I was really confused. Comments never get executed. That can't be wrong. This is strange.

Here is my code:

public void gameOver () {
    tvScore.setText (Integer.toString (Game.getInstance ().getScore ()));
    tvHighscore.setText (Game.getInstance ().getHighscore ());
    tvScoreText.setVisibility (View.VISIBLE);
    tvScore.setVisibility (View.VISIBLE);

    Animation anim = AnimationUtils.loadAnimation (this, R.anim.cover_fade_in);
    anim.setAnimationListener (new Animation.AnimationListener () {
        @Override
        public void onAnimationStart(Animation animation) {
            GameActivity.this.cover.setVisibility (View.VISIBLE);
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            GameActivity.this.cover.setVisibility (View.VISIBLE);
            Game.InitializeGame (GameActivity.this);
            cover.setVisibility (View.VISIBLE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    cover.startAnimation (anim);
}

The line tvHighscore.setText (Game.getInstance ().getHighscore ()); refers to the line in the call stack: at com.whackanandroid.GameActivity.gameOver(GameActivity.java:68). I think it might be because tvHighscore's parent view, a LinearLayout is GONE. Does this matter? Or did I do anything else wrong?

If you need to see more code, feel free to ask me.

3条回答
Emotional °昔
2楼-- · 2019-09-17 16:50

Or did I do anything else wrong?

Yes. You called setText(int), with a value that is not a string resource ID. Change:

tvHighscore.setText (Game.getInstance ().getHighscore ());

to:

tvHighscore.setText(Integer.toString(Game.getInstance().getHighscore()));
查看更多
SAY GOODBYE
3楼-- · 2019-09-17 16:53

The problem is that you are using the setText with an integer parameter, this means a resource id.

To solve your problem you could do this:

tvHighscore.setText(""+Game.getInstance().getHighscore());
查看更多
萌系小妹纸
4楼-- · 2019-09-17 17:02

If the jar is pulled in as a dependency and you are debugging that then the lines are from the class file that is created by the complier and not java class it self you have to go my the method name and other information. You cannot always rely on the line number.

Another reason can be different version of current source that you are using to see line number and jar that is pulled in is of different version.

查看更多
登录 后发表回答