使用setText

时间:2016-03-13 20:44:04

标签: java android

我在使用成绩百分比计算器时遇到问题。该项目有两个布局和两个java类。根据logcat,问题在于First_Screen_J Java类。这就是logcat所说的:

  

03-13 13:31:24.869 2167-2167 / com.example.luke.percentagegradecalculator E / AndroidRuntime:FATAL EXCEPTION:main进程:com.example.luke.percentagegradecalculator,PID:2167 java.lang.RuntimeException:Unable启动活动ComponentInfo {com.example.luke.percentagegradecalculator / com.example.luke.percentagegradecalculator.First_Screen_J}:android.content.res.Resources $ NotFoundException:String resource ID#0x13

  引起:android.content.res.Resources $ NotFoundException:字符串资源ID#0x13                                                                                                         在android.content.res.Resources.getText(Resources.java:312)的android.widget.TextView.setText(TextView.java:4417)at com.example.luke.percentagegradecalculator.First_Screen_J.onCreate(First_Screen_J.java:79 )在android.app.Activity.performCreate(Activity.java:6237)

public class First_Screen_J extends Activity {

    int MaxInt;
    String number = null;

    private TextView TV1;
    ....
    private TextView TV24;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firstscreenx);

        TV1 = (TextView) findViewById(R.id.textView);
        ....
        TV24 = (TextView) findViewById(R.id.textView24);

        Bundle extras = getIntent().getExtras();
        if (extras != null){
            number = extras.getString("Number");
        }

        MaxInt = Integer.parseInt(number);
        TV1.setText(MaxInt - 1); // << --- Line Number 79 --- >>
        ...
        TV8.setText(MaxInt - 8);
        TV9.setText((int) Math.round((MaxInt - 1) / MaxInt) + "%");
        ...
        TV16.setText((int) Math.round((MaxInt - 8) / MaxInt) + "%");
    }
}

请帮帮我!谢谢!

1 个答案:

答案 0 :(得分:3)

您滥用TextView.setText电视1 - &gt; 8

TV1.setText(MaxInt - 1);

原因是你正在使用

int x = MaxInt - 1;
TV1.setText( x );

Android Documentation for setText(int i)i将是一个ResourceID,一个XML文件中的String定义,将使用 R.string.text_for_tv1 或其他东西引用。

要解决此问题,只需使用setText(CharSequence cs),通过预先"" +

将您的值转换为字符串
TV1.setText("" + MaxInt - 1);

或者

 TV1.setText(Integer.toString(MaxInt - 1));

您似乎试图显示数字19,即转换的十六进制值0x13。抛出错误是因为在XML中找不到ID为 0x13 的字符串值。

其余的textvews会正常工作,因为你在整数之后有+ "%",如果不存在,你会遇到同样的问题。