0

I've got a problem: when I try to debug my app in my device, it just instantly closes it after launch.

Here's the code:

public class Main extends AppCompatActivity {
    EditText convFromET, convToET, commaNumbET;
    Spinner  selectCatS, convFromS, convToS;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        convFromET = (EditText)findViewById(R.id.input_convert_from);
        convToET = (EditText)findViewById(R.id.input_convert_to);
        commaNumbET = (EditText)findViewById(R.id.input_comma_numbers);

        selectCatS = (Spinner)findViewById(R.id.spinner_select_category);
        convFromS = (Spinner)findViewById(R.id.spinner_convert_from);
        convToS = (Spinner)findViewById(R.id.spinner_convert_to);

        SetSelectCatSOnClickListener( );


    }

    private void SetSelectCatSOnClickListener( ){
        selectCatS.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ArrayAdapter<CharSequence> adapter;

                switch( position ) {
                    case 0: // length
                        adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.spinner_length, android.R.layout.simple_spinner_item);
                        break;

                    case 1: // speed
                        adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.spinner_speed, android.R.layout.simple_spinner_item );
                        break;

                    case 2: //tempreture
                        adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.spinner_tempreture, android.R.layout.simple_spinner_item );
                        break;

                    case 3: // volume
                        adapter = ArrayAdapter.createFromResource( getApplicationContext(), R.array.spinner_volume, android.R.layout.simple_spinner_item );
                        break;

                    default: // Weigth
                        adapter = ArrayAdapter.createFromResource( getApplicationContext(), R.array.spinner_weigth, android.R.layout.simple_spinner_item );
                        break;
                }

                adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
                convFromS.setAdapter( adapter );
                convToS.setAdapter( adapter );
            }
        });
    }
}

And here's the XML file ( strings.xml ):

<string name="select_category">Choose a category</string>
<string name="convert_from">Convert from</string>
<string name="convert_to">Convert to</string>
<string name="numbers_after_comma">Numbers after comma</string>

<string-array name="spinner_categories">
    <item>Length</item>
    <item>Weigth</item>
    <item>Volume</item>
    <item>Tempreture</item>
    <item>Speed</item>
    <item>Volume</item>
</string-array>

<string-array name="spinner_length">
    <item>Inches</item>
    <item>Feets</item>
    <item>Centimeters</item>
    <item>Meters</item>
    <item>Kilometers</item>
</string-array>

<string-array name="spinner_speed">
    <item>Meters per second</item>
    <item>Kilometers per hour</item>
    <item>Miles per hour</item>
</string-array>

<string-array name="spinner_tempreture">
    <item>Celsius</item>
    <item>Farenheith</item>
    <item>Kelvin</item>
</string-array>

<string-array name="spinner_volume">
    <item>Cubic milimeters</item>
    <item>Cubic centimeters</item>
    <item>Cubic meters</item>
    <item>Liters</item>
</string-array>

<string-array name="spinner_weigth">
    <item>Miligrams</item>
    <item>Grams</item>
    <item>Kilograms</item>
    <item>Pounds</item>
    <item>Tons</item>
</string-array>

By the way, when I comment SetSelectCatSOnClickListener() in onCreate, the app doesn't crash, so I assume that problem must be there.

Thanks in advance!

Sw3das
  • 157
  • 1
  • 12

2 Answers2

0

Its not OnItemClick it should be onItemSelected for the spinner

Ram Mandal
  • 1,899
  • 1
  • 20
  • 35
-1

To find out specifics on the error with logcat.

On eclipse:

Go to Window -> Show View -> Android -> logcat.

Logcat is then pinned to the bottom of you screen.

You will see a drop down that has verbose by default. Change that to error. Now you will only see errors in Logcat.

Once you have the error post it here and I am sure we can help you.

SID --- Choke_de_Code
  • 1,131
  • 1
  • 16
  • 41
TerNovi
  • 390
  • 5
  • 16
  • Another thing, I just read your code. If you are beginner in android. I suggest you extend your Main class to just Activity. Instead of AppCompatActivity, just to avoid unnecessary stuff. – TerNovi Jul 14 '15 at 21:50
  • refer to this post because you cannot use onItemClickListener on a Spinner. http://stackoverflow.com/questions/15064498/setonitemclicklistener-not-works-with-spinner – TerNovi Jul 14 '15 at 21:51
  • Actually you're wrong. `AppCompatActivity` is a class provided in the latest android support library. Refer to this post for more information: http://stackoverflow.com/questions/29797172/whats-the-enhancement-of-appcompatactivity-over-actionbaractivity – VulfCompressor Jul 15 '15 at 01:30
  • I never said it was wrong. Notice I said "If you are a beginner". No need to have a bunch of implemented methods and extra stuff happening when you are just learning the basics. – TerNovi Jul 16 '15 at 02:05
  • Even though he is a beginner, at some point he will need to learn how to use the AppCompat Library, and extending AppCompatActivity doesn't demand any effort on "method implementations and extra stuff". Besides, there isn't any downsides in learning the right way, huh ? – VulfCompressor Jul 16 '15 at 02:41
  • Would it be easier to understand what an Activity is, versus a ListActivity, FragmentActivity, etc... . It's always easier to dive in with simplicity to understand the full picture. Either way, most resources (books, online tutorials) extend the Activity class. Ask yourself why they might do that? – TerNovi Jul 16 '15 at 13:40