如何使用StringUtils的myString的和,越来越无法解析错误 [英] How to use StringUtils and myString, getting cannot resolve error

查看:843
本文介绍了如何使用StringUtils的myString的和,越来越无法解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行低于code和无法解决StringUtils的myString的和不...需要的东西导入或有另一种方式?这需要从另一个活动的字符串,并使其成为一个整数,让新旧值的计算。

 保护无效的onCreate(捆绑savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.activity_main);            //设置按钮的唯一名称
            按钮buttonAdd =(按钮)findViewById(R.id.add_button);
            按钮buttonAbout =(按钮)findViewById(R.id.about_button);
            按钮buttonReset =(按钮)findViewById(R.id.reset_button);
            字符串热量= getIntent()getStringExtra(卡路里);
            TextView的textView1 =(的TextView)findViewById(R.id.textView1);            字符串strOldValue = textView1.getText()的toString()。            整数属性oldValue = StringUtils.isNotBlank(MyString的)?的Integer.parseInt(MyString的):0;
            整数为newValue =的Integer.parseInt(卡路里);            textView1.setText((属性oldValue +为newValue));


解决方案

最安全的方法是使用尝试{}赶上{} 阻止像code以下

 整数属性oldValue;
尝试{
 属性oldValue =的Integer.parseInt(myString的);
}赶上(NumberFormatException的E){
  属性oldValue = 0;
}

而不是:

 整数属性oldValue = StringUtils.isNotBlank(MyString的)?的Integer.parseInt(MyString的):0;

Running the below code and can't resolve StringUtils and myString... does something need imported or is there another way? this takes a string from another activity and makes it an integer to allow a calculation of old and new values.

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

            // Setting button as a unique name
            Button buttonAdd = (Button) findViewById(R.id.add_button);
            Button buttonAbout = (Button) findViewById(R.id.about_button);
            Button buttonReset = (Button) findViewById(R.id.reset_button);


            String calorie = getIntent().getStringExtra("calorie");
            TextView textView1 = (TextView)findViewById(R.id.textView1);

            String strOldValue = textView1.getText().toString();

            Integer oldValue = StringUtils.isNotBlank(myString) ? Integer.parseInt(myString) : 0;
            Integer newValue = Integer.parseInt(calorie);



            textView1.setText((oldValue + newValue));

解决方案

The most of safe way is used to try {} catch {} block like code below

Integer oldValue;
try{
 oldValue= Integer.parseInt(myString);
}catch(NumberFormatException e){
  oldValue =0;
}

instead of :

 Integer oldValue = StringUtils.isNotBlank(myString) ? Integer.parseInt(myString) : 0;

这篇关于如何使用StringUtils的myString的和,越来越无法解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆