从看似合理的方法看Android的死亡率

时间:2013-11-23 18:29:34

标签: java android exception

我正在开发一个简单的Android项目,但遇到了一个可怕的致命异常,并且没有真正的专业知识来确定我是否在Android世界中做了一些不受限制的事情......  我有这段代码:

private OnClickListener butonList_start = new OnClickListener(){
    public void onClick(View v){
        Toast.makeText(getBaseContext(), "Good Luck.", Toast.LENGTH_LONG).show();

        otherBtn_1.setOnClickListener(otherListener_1);
        otherBtn_2.setOnClickListener(otherListener_2);

        aTextView.setTextSize(30);
        aTextView.setText("");
        aTextView.setMaxLines(1);
    }
};

所有这一切都顺利执行,直到最后我输入:

activate();

这是对此代码的引用:

private void activate(){
    setImage_AS();
    first = selector1_AS();
    second = selector2_AS();
    aTextView_2.setText(first);
    aTextView_3.setText(second);

}

private void setImage_AS(){
    int xxx = randm.nextInt(2);
    if(xxx==0){
        image.setImageResource(R.drawable.crew_31242514);
        int op = 0;
    }
    if(xxx==1){
        image.setImageResource(R.drawable.ic_launcher);
        int op = 1;
    }
}


private int selector1_AS(){
    return randm.nextInt(50);
}
private int selector2_AS(){
    return randm.nextInt(first);
}

通过“激活”方法,我设置了预定义ImageView的图像,并通过各自的方法给出“第一”和“第二”值,这些方法在这些参数中随机分配值。

现在,除非用户按下按钮,否则此后不会发生任何其他事情,因此,错误应该在这里吗?我做错了什么,我已经看了这么多,但是无法从LogCat得到任何提示,只返回FATAL EXCEPTION而没有细节(除非我看不到它们)。

我的代码没有错误,因此没有对尚未声明的对象或类型的引用,并且所有内容都在应用程序中平稳运行,直到单击激活butonList_start的按钮为止。请帮帮我!

这是LogCat行 enter image description here

发现问题 还有点困惑

这个问题是我试图将TextView的文本设置为'first'或'second'给出的值的产物,这是一个int ...但是为什么会出现问题,将setText()只接受字符串?

1 个答案:

答案 0 :(得分:2)

在activate()方法中查看Basic.java的第63行。您正在尝试将文本视图的文本设置为int。我猜这是行aTextView_2.setText(first);

因此,TextView有一个方法setText(int),期望int成为字符串资源id。没有与您提供的id匹配的字符串资源,因此抛出异常。

如果要在TextView中显示整数值,请尝试使用myTextView.setText( String.valueOf( someInteger ) );之类的内容。此方法setText(CharSequence)采用CharSequence,允许您直接指定要在TextView中显示的文本