为什么这个调用栈这么奇怪?

Why is this call stack so weird?

今天我正在调试我的 android 应用程序,但它崩溃了。这是调用堆栈:

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.onCountDownFinished(Game.java:79)
        at com.whackanandroid.CountDown.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)

然后我单击了行号链接 (TextView.java:3860),它把我带到了一行 javadoc 注释。我真的很困惑。评论 永远不会 被执行。那不会错的。这很奇怪。

这是我的代码:

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);
}

tvHighscore.setText (Game.getInstance ().getHighscore ());指的是调用堆栈中的行:at com.whackanandroid.GameActivity.gameOver(GameActivity.java:68)。我想可能是因为tvHighscore的父视图,一个LinearLayoutGONE。这有关系吗?还是我做错了什么?

如果您需要查看更多代码,请随时询问我。

如果 jar 作为依赖项被拉入并且您正在调试,那么这些行来自编译器创建的 class 文件而不是 java class 它self 你必须去我的方法名称和其他信息。您不能总是依赖行号。

另一个原因可能是您用来查看行号的当前源版本不同,而拉入的 jar 版本不同。

Or did I do anything else wrong?

是的。您调用了 setText(int),其值不是字符串资源 ID。变化:

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

至:

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

问题是您正在使用带有整数参数的 setText,这表示资源 ID。

要解决您的问题,您可以这样做:

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