如何在android中将字符串转换为整数?

时间:2016-11-02 04:24:20

标签: java android

我想将userid(字符串)转换为整数,但是app会崩溃为什么? 如果我删除转换后的方法然后没有崩溃但我想要整数值我做什么?

  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();
        }

如果我删除转换功能然后应用程序没有崩溃,但我想要整数值我做什么?

1 个答案:

答案 0 :(得分:1)

编辑:

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