我试图用吐司测试我的微调器的功能。我遇到了麻烦,因为它没有烘烤ShowTime,而是强行关闭。我的logcat发布在代码下面。如果有人可以帮我读取我的logcat代码并弄清楚什么是错误的。提前谢谢!
public class Spinner extends Activity {
Spinner bus;
String GetTime;
int ShowTime;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] Log_Array = new String[9];
{
Log_Array[0] = "5 min";
Log_Array[1] = "10 min";
Log_Array[2] = "30 min";
Log_Array[3] = "50 min";
Log_Array[4] = "100 min";
Log_Array[5] = "300 min";
Log_Array[6] = "400 min";
Log_Array[7] = "500 min";
Log_Array[8] = "600 min";
bus= (Spinner) findViewById(R.id.timer);
ArrayAdapter NoCoreAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, Log_Array);
bus.setAdapter(NoCoreAdapter);
bus.setSelection(1);
}
bus.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
GetTime = parent.getItemAtPosition(pos).toString();
if (pos == 0) {
GetTime.equals(Log_Array[0]);
ShowTime = 5;
}
if (pos == 1) {
GetTime.equals(Log_Array[1]);
ShowTime = 5;
}
if (pos == 2) {
GetTime.equals(Log_Array[2]);
ShowTime = 5;
}
if (pos == 3) {
GetTime.equals(Log_Array[3]);
ShowTime = 5;
}
if (pos == 4) {
GetTime.equals(Log_Array[4]);
ShowTime = 5;
}
if (pos == 5) {
GetTime.equals(Log_Array[5]);
ShowTime = 5;
}
if (pos == 6) {
GetTime.equals(Log_Array[6]);
ShowTime = 5;
}
if (pos == 7) {
GetTime.equals(Log_Array[7]);
ShowTime = 5;
}
if (pos == 8) {
GetTime.equals(Log_Array[8]);
ShowTime = 6;
}
Toast.makeText(getApplicationContext(), ShowTime,
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
});
}
我的LogCat
07-01 16:12:14.348: E/AndroidRuntime(21733): FATAL EXCEPTION: main
07-01 16:12:14.348: E/AndroidRuntime(21733): android.content.res.Resources$NotFoundException: String resource ID #0x5
07-01 16:12:14.348: E/AndroidRuntime(21733): at android.content.res.Resources.getText(Resources.java:210)
07-01 16:12:14.348: E/AndroidRuntime(21733): at android.widget.Toast.makeText(Toast.java:258)
07-01 16:12:14.348: E/AndroidRuntime(21733): at com.settings.Settings$1.onItemSelected(Settings.java:88)
07-01 16:12:14.348: E/AndroidRuntime(21733): at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
07-01 16:12:14.348: E/AndroidRuntime(21733): at android.widget.AdapterView.access$200(AdapterView.java:42)
07-01 16:12:14.348: E/AndroidRuntime(21733): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
07-01 16:12:14.348: E/AndroidRuntime(21733): at android.os.Handler.handleCallback(Handler.java:587)
07-01 16:12:14.348: E/AndroidRuntime(21733): at android.os.Handler.dispatchMessage(Handler.java:92)
07-01 16:12:14.348: E/AndroidRuntime(21733): at android.os.Looper.loop(Looper.java:143)
07-01 16:12:14.348: E/AndroidRuntime(21733): at android.app.ActivityThread.main(ActivityThread.java:4196)
07-01 16:12:14.348: E/AndroidRuntime(21733): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 16:12:14.348: E/AndroidRuntime(21733): at java.lang.reflect.Method.invoke(Method.java:507)
07-01 16:12:14.348: E/AndroidRuntime(21733): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-01 16:12:14.348: E/AndroidRuntime(21733): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-01 16:12:14.348: E/AndroidRuntime(21733): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
Toast有一个方便的方法,允许您传递一个资源ID(整数),它是一个指向字符串资源的指针。
所以这行代码:
Toast.makeText(getApplicationContext(), ShowTime,Toast.LENGTH_LONG).show();
告诉程序进行Toast并使用整数ShowTime
为其ID的String资源填充它。在您的项目中,String资源不存在,因此您将收到此异常。
要解决此问题,我需要使用makeText()方法,该方法将String作为第二个参数,而不是int。像这样:
Toast.makeText(Spinner.this, String.ValueOf(ShowTime),Toast.LENGTH_LONG).show();
另请注意,您的Activity 是上下文,因此您应该使用this
(您的活动)而不是getApplicationContext()
方法。并且因为您在Listener对象声明中,您需要将您的Activity名称放在this
编辑:我还建议您不要将自己的活动命名为“Spinner&#34;”。由于存在一个名为Spinner的UI组件对象,因此会增加一定程度的connfusion。如果我是你,我会重构它并将Activity的名称更改为SpinnerActivity。