Im trying to test the functionality of my spinner with a toast. I am having trouble because instead of toasting ShowTime, it force closes. my logcat is posted below the code. If anybody can help me read my logcat code and figure out whats wrong. Thanks in advance!
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.
}
});
}
My 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)
Toast has a convenience method that allows you to pass it a resource ID(integer) that is a pointer to a string resource.
so this line of code:
is telling the program to make a Toast and fill it with the String resource that the Integer
ShowTimeis the ID for. In your project that String resource doesn’t exist so you are getting this exception.To fix you’ll need to use the makeText() method that takes a String as the second parameter, rather than an int. Like this:
Also note that your Activity is a context, so you should be using
this(your activity) instead of thegetApplicationContext()method. And because you are inside of a Listener object declaration you need to put the name of your Activity infront ofthisEdit: I would also suggest that you do not name your activity “Spinner”. Since there is a UI component object that is called Spinner this adds a level of connfusion. I would refactor it and change the name of the Activity to SpinnerActivity if I were you.