I set the task to realize the choice of year, month and day not by means of libraries, but by hand. What am I wrong?

Error log

08-10 19: 59: 13.906 21295-21295 / game.life.ru.basicviews E / AndroidRuntime: FATAL EXCEPTION: main Process: game.life.ru.basicviews, PID: 21295 java.lang.RuntimeException: Unable to start activity ComponentInfo {game.life.ru.basicviews / game.life.ru.basicviews.MainActivity}: java.lang.NumberFormatException: Invalid int: "android.widget.NumberPicker {1f23bb19 V.ED .... ..... .I. 0.0-0.0 # 7f0b0056 app: id / numberPicker3} "at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2320) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2380) at android.app.ActivityThread.access $ 800 (ActivityThread.java:151) at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1285) at android.os.Handler.dispatchMessage (Handler.java:102) at android. os.Looper.loop (Looper.java:135) at android.app.ActivityThread.main (ActivityThread.javaive296) at java.lang.reflect.Method.invoke (Native Method) at java.lang.reflect.Method. Invoke (Method.javaUE72) at com.android.internal.os.Zygot eInit $ MethodAndArgsCaller.run (ZygoteInit.java:899) at com.android.internal.os. ZygoteInit.main (ZygoteInit.java:694) 1f23bb19 V.ED .... ...... I. 0.0-0.0 # 7f0b0056 app: id / numberPicker3 "at java.lang.Integer.invalidInt (Integer.java:138) at java.lang.Integer.parse (Integer.java:410) at java.lang .Integer.parseInt (Integer.java天67) at java.lang.Integer.parseInt (Integer.javaUE34) at game.life.ru.basicviews.MainActivity.onCreate (MainActivity.java:31) at android.app. Activity.performCreate (Activity.java:6018) at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2273) at android.app.ActivityThread.handleLaunchActivity (ThisThread .java: 2380) at android.app.ActivityThread.access $ 800 (ActivityThread.java:151) at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1285) at android.os.Handler.dispatchMessage (Handler.java : 102) at android.os.Looper.loop (Looper.java:135) at android.app.ActivityThread.main (ActivityThread.javaClt296) at java.lang.reflect.Method.invoke (Native Method) at java. lang.reflect.Method.invoke (Method.javaUE7272) at co m.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:694)

Program code

package game.life.ru.basicviews; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.NumberPicker; public class MainActivity extends AppCompatActivity { public NumberPicker yearChoose ; public NumberPicker monthChoose ; public int maxDay = 31; public int maxYear = 2016; public int minYear = 1940; public int maxMonth = 12; public int minMonth = 1; public int minDay = 1; public String yearChoseStr = ""; public int yearChoseInt = 0; public String monthChoseStr = ""; public int monthChoseInt = 0; public int modif = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); NumberPicker numberPicker3 = (NumberPicker) findViewById(R.id.numberPicker3); numberPicker3.setMaxValue(maxYear); numberPicker3.setMinValue(minYear); yearChoose = (NumberPicker)findViewById(R.id.numberPicker3); yearChoseStr = yearChoose.toString(); yearChoseInt = Integer.parseInt(yearChoseStr); if(yearChoseInt%4==0 && yearChoseInt%100 !=0 || yearChoseInt%400==0){//проверка высокосности года modif = modif+1; } else { modif = 0; } NumberPicker numberPicker2 = (NumberPicker) findViewById(R.id.numberPicker2); numberPicker2.setMaxValue(maxMonth); numberPicker2.setMinValue(minMonth); monthChoose = (NumberPicker)findViewById(R.id.numberPicker2); monthChoseStr = monthChoose.toString(); monthChoseInt = Integer.parseInt(monthChoseStr); switch (yearChoseInt) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: maxDay = 31; break; case 4: case 6: case 9: case 11: maxDay = 30; break; case 2: default: maxDay = 28 + modif; } NumberPicker numberPicker = (NumberPicker) findViewById(R.id.numberPicker); numberPicker.setMaxValue(maxDay); numberPicker.setMinValue(minDay); } } 

    1 answer 1

    Let's read the template.

    java.lang.NumberFormatException tells us that you are trying to convert a string that is not a representation of a number into a number. And we are told how exactly this line looked:

     Invalid int: "android.widget.NumberPicker{1f23bb19 V.ED.... ......I. 0,0-0,0 #7f0b0056 app:id/numberPicker3}" 

    Here it becomes obvious that the toString() method was called on an object of the NumberPicker class. Then we find out where it happened:

     at game.life.ru.basicviews.MainActivity.onCreate(MainActivity.java:31) 

    on the 31st line in the MainActivity.java file, in the onCreate method of the onCreate class.

    And indeed, there we see the following:

     yearChoose = (NumberPicker)findViewById(R.id.numberPicker3); yearChoseStr = yearChoose.toString(); yearChoseInt = Integer.parseInt(yearChoseStr); 

    You get the NumberPicker object and for some reason call on it .toString() , while it has a getValue() method that will immediately return the number entered by the user.