代码之家  ›  专栏  ›  技术社区  ›  Sweeper

为什么这个调用堆栈如此奇怪?

  •  0
  • Sweeper  · 技术社区  · 9 年前

    今天我在调试我的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的家长观点 LinearLayout GONE 。这有关系吗?还是我做错了什么?

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

    3 回复  |  直到 9 年前
        1
  •  5
  •   CommonsWare    9 年前

    还是我做错了什么?

    对你打过电话 setText(int) ,值不是字符串资源ID。更改:

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

    至:

    tvHighscore.setText(Integer.toString(Game.getInstance().getHighscore()));
    
        2
  •  0
  •   StackFlowed    9 年前

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

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

        3
  •  0
  •   thiagolr    9 年前

    问题是您正在使用 setText 对于整数参数,这意味着资源id。

    要解决您的问题,您可以执行以下操作:

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