user6405383 user6405383 - 1 month ago 5
Java Question

How to convert string to integer in android?

i want to convert userid(string) to integer but app get to crashed why?
if i removes converted method then no crashed but i want integer value what i do?

public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
handler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();


/**
* Get stored session data userid from other class
* */
userid = "";
session = new SessionManager(getApplicationContext());//<-- this
HashMap<String, String> user = session.getUserDetails();
userid = user.get(SessionManager.KEY_userid);

//i want convert userid to int, when i try to
convert then app is carashed. pls help me what i am missing here

Integer U_ID = Integer.valueOf(userid);
if(U_ID >0){
Toast.makeText(GcmMessageHandler.this, "new on id="+userid, Toast.LENGTH_LONG).show();

}


if i removes convert function then app not crashed, but i want integer valued what i do?

Answer

EDIT:

if(userid != null && !userid.trim().isEmpty()){
     int  U_ID = Integer.parseInt(userid)
}
Comments