当前位置: 动力学知识库 > 问答 > 编程问答 >

java - How to use StringUtils and myString, getting cannot resolve error

问题描述:

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;
网友答案:

To add to Konrad's answer:

You should include all your code...

if you are trying to Get a String from a previous Activity then parse it to an Integer, Why not just pass it as an int And do something like:

Declare class level variable

private int mPassedInValue;

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

 // Get the Extras Bundle from the Intent
 Bundle previousIntentExtras  = getIntent().getExtras();

 // Check to make sure this is not null
 if(previousIntentExtras != null){
    mPassedInValue = previousIntentExtras.getInt("MyInt",0);

  /* ... Perform the function you want to do since the value you passed in is
   * Is already in the correct form...
   */ 

 }

} 
分享给朋友:
您可能感兴趣的文章:
随机阅读: