问题
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));
回答1:
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;
回答2:
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...
*/
}
}
来源:https://stackoverflow.com/questions/28726962/how-to-use-stringutils-and-mystring-getting-cannot-resolve-error