【问题标题】:Why is this call stack so weird?为什么这个调用堆栈如此奇怪?
【发布时间】:2015-10-23 05:38:59
【问题描述】:

今天我正在调试我的 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$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)

然后我点击了行号链接 (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。这有关系吗?还是我做错了什么?

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

【问题讨论】:

  • 当前源和编译时源的不同版本 - 正常。
  • Android 正在尝试查找具有 int 值的资源 tvHighscore.setText (Game.getInstance ().getHighscore ());

标签: java android callstack


【解决方案1】:

还是我做错了什么?

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

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

到:

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

【讨论】:

  • 哦,是的!非常感谢!
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    问题是您使用带有整数参数的setText,这意味着资源ID。

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

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

    【讨论】:

    • 这里不需要连接字符串,最好的建议是用 String.valueOf() 包装
    猜你喜欢
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 2017-02-09
    相关资源
    最近更新 更多