解析时显示错误。 我只想传递用户获得的整数值。 但它在解析时显示错误。 因此,纠正如何接受用户的整数值以及如何以整数格式传递它的问题。 编码:
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();
}
edittext的xml部分
<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" />
堆栈跟踪:
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)
答案 0 :(得分:1)
更改为
int l = Integer.parseInt(s);
tv1.setText(String.valueOf(l));
setText
需要CharacterSequence
。但是,还有一种方法将resourceid作为参数。在你的情况下,setText采用了int。如果android没有找到具有提到的id的资源,则会获得ResourceNotFoundException