1

My application forced closed when I try to convert the editText String to integer and I don't know why. I think i'm converting it right. What supposed to be the problem? Thanks in advance.

edX = (EditText) findViewById (R.id.dh);

path.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                String holder = edX.getText().toString();
                int a = Integer.parseInt(holder);
                Toast.makeText(getApplicationContext(), a, Toast.LENGTH_SHORT).show();
            }   
        });
3
  • 1
    where is your logcat? Commented Feb 19, 2015 at 4:49
  • I can't copy paste my log cat at android.os.Handler.handleCallback(Handler.java:800) at pathfinding.thesis.algorith.PathSetup$1.onClick(PathSetup.java:73) Commented Feb 19, 2015 at 4:51
  • follow the below given answers then or delete your question. Commented Feb 19, 2015 at 4:52

3 Answers 3

2

Probably getting :

Resources$NotFoundException: String resource ID

Because Toast makeText takes CharSequence as second parameter.

Use String.valueOf to get String representation of int:

Toast.makeText(getApplicationContext(), 
     String.valueOf(a), Toast.LENGTH_SHORT).show();
Sign up to request clarification or add additional context in comments.

1 Comment

Why Down-Vote ? please kindly leave comments with down-vote to improve my answer and for learn .Thanks
1

Modify your toast as below :

Toast.makeText(getApplicationContext(), String.valueOf(a), Toast.LENGTH_SHORT).show();

Comments

0

You are facing

android.content.res.Resources$NotFoundException: Resource ID #0x0

Use my code below , It will definetely solve your problem.

path.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            String holder = edX.getText().toString();
            int a=-1;
            try {
                a = Integer.parseInt(holder);
            } catch (NumberFormatException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(a==-1){
                Toast.makeText(getApplicationContext(), "INVALID NUMBER INPUT", Toast.LENGTH_SHORT).show();
            }else{                  
                Toast.makeText(getApplicationContext(), a+"", Toast.LENGTH_SHORT).show();
            }
        }   
    });

The hack is , Toast class can only accept integer resource ids and string object . I converted int to String :) .

Comments

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.