androidandroid-parser

how to pass an integer value using number type Edittext?


Shows error while parsing. I just want to pass the integer value obtained by user. But it shows error while parsing. So Moreover rectify the issue that how to accept the integer value from user and how do i pass it in Integer format. Coding:

String s=et1.getText().toString();
if(s.matches("")){
    Toast t1= Toast.makeText(MainActivity.this, "Empty", 2000);
    t1.show();
}
else{
    int l = Integer.parseInt(s);
    Toast t2= Toast.makeText(MainActivity.this, s, 2000);
    t2.show();
    tv1.setText(l);
    Intent i1= new Intent(Loadscreen.this,Game01.class);
    i1.putExtra("l", l);
    startActivity(i1);
    Loadscreen.this.finish();
}

The xml part of edittext

<EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_x="0dp"
        android:layout_y="219dp"
        android:background="#00000000"
        android:ems="10"
        android:gravity="center_vertical"
        android:hint="Specify Level"
        android:inputType="number" />

StackTrace:

02-28 20:06:24.332: E/AndroidRuntime(647): FATAL EXCEPTION: main
02-28 20:06:24.332: E/AndroidRuntime(647): android.content.res.Resources$NotFoundException: String resource ID #0x5 
02-28 20:06:24.332: E/AndroidRuntime(647): at android.content.res.Resources.getText(Resources.java:201) 
02-28 20:06:24.332: E/AndroidRuntime(647): at android.widget.TextView.setText(TextView.java:2857)   
02-28 20:06:24.332: E/AndroidRuntime(647): at com.example.abc.MainActivity$1.onClick(MainActivity.java:37) 
02-28 20:06:24.332: E/AndroidRuntime(647): at android.view.View.performClick(View.java:2485)
02-28 20:06:24.332: E/AndroidRuntime(647): at android.view.View$PerformClick.run(View.java:9080) 
02-28 20:06:24.332: E/AndroidRuntime(647): at android.os.Handler.handleCallback(Handler.java:587) – Shubhankar 2 mins ago       
02-28 20:06:24.332: E/AndroidRuntime(647): at android.os.Handler.dispatchMessage(Handler.java:92) 
02-28 20:06:24.332: E/AndroidRuntime(647): at android.os.Looper.loop(Looper.java:123) 
02-28 20:06:24.332: E/AndroidRuntime(647): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-28 20:06:24.332: E/AndroidRuntime(647): at java.lang.reflect.Method.invokeNative(Native Method) 
02-28 20:06:24.332: E/AndroidRuntime(647): at 

java.lang.reflect.Method.invoke(Method.java:507)


Solution

  • Change to

    int l = Integer.parseInt(s);
    tv1.setText(String.valueOf(l));
    

    setText takes CharacterSequence. However there is also a method that takes resourceid as a param. In your case setText took int. If android does not find the resource with the id mentioned you get ResourceNotFoundException