0

I am getting an error message within logcat that I cannot figure out. It is a NumberFormatException. How can I post the log error to stack overflow so that it can be seen?

Also how can you find the exact line where the error occurs using logcat?

Class in which the error may be:

public class Practice extends Activity implements View.OnClickListener {

    // Declaring Vars
    Button go2;
    EditText enterNumber2;
    TextView top2;
    TextView bottom2;
    int convertedNumber2; //change back to private Integer?

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // setting equal to text layout View
        setContentView(R.layout.practice);

        // calling method to intialise vars
        initialiseVars();

        // setting on click listeners for edit text and button
        go2.setOnClickListener(this);
        enterNumber2.setOnClickListener(this);

    }// on create end

    /**
     * method to initialise all of the buttons, textviews etc used to clean up
     * the onCreate.
     */
    private void initialiseVars() {
        // Setting up (initialising) all the buttons text views etc from the xml
        // (vid 25)
        go2 = (Button) findViewById(R.id.btnGoPractice);
        enterNumber2 = (EditText) findViewById(R.id.etEnterNumberPractce);
        top2 = (TextView) findViewById(R.id.tvTopPractice);
        bottom2 = (TextView) findViewById(R.id.tvBottomPractice);

    }

    /**
     * Method with on click listener that adds functionality for all of the
     * buttons, text views etc
     * 
     * @param v
     */
    public void onClick(View view) {

        // switch statement which determines what is clicked
        switch ((view).getId()) {
        case R.id.btnGoPractice:

            // sets text view equal to whats typed in in editText
            final String entry = enterNumber2.getText().toString();

            // convert from string value to int
            convertedNumber2 = Integer.parseInt(entry);

            Intent intent = new Intent(this, PracticeTest.class);
            if (convertedNumber2 >= 1 && convertedNumber2 <= 12) {
                intent.putExtra("convertedNumber2", convertedNumber2);
                startActivity(intent);
            } else {
                System.out.println("Invalid number");
            }

            break;
        default:
            System.out.println("invalid entry");
            break;

        }

    }

}// c end

The full logcat report for the error:

04-28 18:14:01.533: E/AndroidRuntime(1560): java.lang.NumberFormatException: Invalid int: " 7"
04-28 18:20:26.169: E/AndroidRuntime(1788): FATAL EXCEPTION: main
04-28 18:20:26.169: E/AndroidRuntime(1788): Process: com.example.multapply, PID: 1788
04-28 18:20:26.169: E/AndroidRuntime(1788): java.lang.NumberFormatException: Invalid int: " 6"
04-28 18:20:26.169: E/AndroidRuntime(1788):     at java.lang.Integer.invalidInt(Integer.java:137)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at java.lang.Integer.parse(Integer.java:374)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at java.lang.Integer.parseInt(Integer.java:365)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at java.lang.Integer.parseInt(Integer.java:331)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at com.example.multapply.MyArrayAdapterPractice.getView(MyArrayAdapterPractice.java:59)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.widget.AbsListView.obtainView(AbsListView.java:2263)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.widget.ListView.onMeasure(ListView.java:1175)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.view.View.measure(View.java:16497)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.view.View.measure(View.java:16497)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.view.View.measure(View.java:16497)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.view.View.measure(View.java:16497)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.view.View.measure(View.java:16497)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.view.Choreographer.doFrame(Choreographer.java:544)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.os.Handler.handleCallback(Handler.java:733)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.os.Looper.loop(Looper.java:136)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at java.lang.reflect.Method.invokeNative(Native Method)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at java.lang.reflect.Method.invoke(Method.java:515)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-28 18:20:26.169: E/AndroidRuntime(1788):     at dalvik.system.NativeStart.main(Native Method)
  • 1
    its probably happening right here `convertedNumber2 = Integer.parseInt(entry);` since thats where you try to convert a string to a number – tyczj Apr 28 '14 at 16:50
  • 1
    Here `convertedNumber2 = Integer.parseInt(entry);` if entry is not a number (i.e.: contains **spaces**, **commas**, **dollar** signs or whatever is **not a cypher**, it won't be parsed as a number). Remove all the **offending characters** before parsing. – Phantômaxx Apr 28 '14 at 16:51
  • read the logcat and see the first line which refer to your code, click and it's what invoke it – Marco Acierno Apr 28 '14 at 16:51
  • will you post your layout plz – Tulsiram Rathod Apr 28 '14 at 17:02
  • What should the line of code be instead then? Thanks –  Apr 28 '14 at 18:05
  • I have added the logcat report to the question, this may help to answer! thanks! –  Apr 28 '14 at 18:26
  • You should also post your MyArrayAdapterPractice.java file. – mpop Apr 28 '14 at 18:52
  • check new posted answer . it resolves . need to uncomment your code this line final String entry = enterNumber2.getText().toString(); – Tulsiram Rathod Apr 28 '14 at 18:52
  • I have tried this and it doesnt work, i made a new question as it includes all the activities that are involved. Please see http://stackoverflow.com/questions/23349403/android-unable-to-locate-error-in-code/23349419?noredirect=1#23349419 –  Apr 28 '14 at 19:19

3 Answers3

0

Your are getting this error cause

533: E/AndroidRuntime(1560): java.lang.NumberFormatException: Invalid int: " 7"

at this line

convertedNumber2 = Integer.parseInt(entry);

variable entry not able to parse cause it contains space

You can use below snippet inside click event .Use try catch block to avoid application force close

//      sets text view equal to whats typed in in editText
//      final String entry = enterNumber2.getText().toString();
        String entry = "5";// get your number this line is for test
        entry = entry.replaceAll("\\D+",""); // remove non-digit character

//      convert from string value to int
        int convertedNumber2 = 0;
        try {
             convertedNumber2 = Integer.parseInt(entry);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

//      Intent intent = new Intent(this, PracticeTest.class);
        if (convertedNumber2 >= 1 && convertedNumber2 <= 12) {
//            intent.putExtra("convertedNumber2", convertedNumber2);
//            startActivity(intent);
             System.out.println("Valid number");
        } else {
            System.out.println("Invalid number");
        }

Provide EditText inputtype i.e android:inputType="number" to avoid entering other character or just use regex to get numbers from string

Use this line to remove all nondigit character from string

entry = entry.replaceAll("\\D+","");
Tulsiram Rathod
  • 1,926
  • 2
  • 19
  • 29
  • This has not solved the problem sorry! I am still getting the error as I got before! –  Apr 28 '14 at 18:03
  • Also, I need to have the line of final String entry = enterNumber2.getText().toString(); as it gets the input that the user gives –  Apr 28 '14 at 18:06
  • I just adsed these snippet for run. I didnt get any exception. So uncomment the above line and use – Tulsiram Rathod Apr 28 '14 at 18:22
  • It does not seem to work for me even as I commented it out, I have added the logcat report in my edit, can you check it please –  Apr 28 '14 at 18:25
  • I tested . now it work if not then refer http://stackoverflow.com/questions/4030928/extract-digits-from-a-string-in-java to remove non digit char from string – Tulsiram Rathod Apr 28 '14 at 18:45
0

The error is apparent there as far as i can see...

java.lang.NumberFormatException: Invalid int: " 6"

There is a space before 6.

There are few fixes i will suggest:-

  1. as i see you are using the edittext as number so probably i expect you wont be taking any non-numberic values so you can directly in the xml for etEnterNumberPractce add android:inputType="number" or something which will allow only numeric values.

  2. you can do like this the parsing

    try { final String entry = enterNumber2.getText().toString().trim(); // note trim() if(!entry.equals("")) convertedNumber2 = Integer.parseInt(entry); // Your rest of the code

    } catch(Exception e) { e.printStackTrace(); }

Hope it helps...

thx

Ahmad
  • 437
  • 1
  • 4
  • 12
0

according to

at com.example.multapply.MyArrayAdapterPractice.getView(MyArrayAdapterPractice.java:59)

it is in the file MyArrayAdapterPractice.java on line 59 I am looking though the stack dump and I don't see Practice in there, it might be called by the android system in another way, but the object in question is MyArrayAdapterPractice in the method getView, and it should be about line 59. Looks like according to the stack trace that it is happening when you are building your list view object.

mpop
  • 499
  • 11
  • 21