-4

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?

6
  • use Integer.parseInt(userid) instead of Integer.valueOf(userid);. If userid is blank, it will not work Commented Nov 2, 2016 at 4:26
  • 1
    What is the exception you get? Commented Nov 2, 2016 at 4:26
  • i did it but still app crashed Commented Nov 2, 2016 at 4:26
  • You should use the debugger to ensure that the value inside of userid can actually be converted into an Integer. Commented Nov 2, 2016 at 4:28
  • 1
    Possible duplicate of Converting a string to an integer on Android Commented Nov 2, 2016 at 4:48

1 Answer 1

2

EDIT:

if(userid != null && !userid.trim().isEmpty()){
     int  U_ID = Integer.parseInt(userid)
}
Sign up to request clarification or add additional context in comments.

1 Comment

please try the sasikumar code ! your problem has been solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.