0

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)

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
Shubhankar
  • 95
  • 1
  • 14
  • The activity responds as force close and error is on line "int l = Integer.parseInt(s);" as specified in logcat. – Shubhankar Feb 28 '14 at 14:32
  • post the stack trace then – Raghunandan Feb 28 '14 at 14:34
  • 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) – Shubhankar Feb 28 '14 at 14:44
  • 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 Feb 28 '14 at 14:44
  • 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) – Shubhankar Feb 28 '14 at 14:45
  • Edit your comment to post stack traces :) – NameSpace Feb 28 '14 at 14:49

1 Answers1

1

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

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • int l = Integer.parseInt(s); Toast t2= Toast.makeText(MainActivity.this, s, 2000); t2.show(); tv1.setText(l); – Shubhankar Feb 28 '14 at 14:48
  • @Shubhankar no its not. – Raghunandan Feb 28 '14 at 14:48
  • This is correct. If you follow this poster's answer, you cannot possibly get the error you got in your stack trace. Right now it thinks your int is a resource ID, because it see's it as an integer. – NameSpace Feb 28 '14 at 14:52