Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: CheckBox/Spinner error Error

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default CheckBox/Spinner error Error

    For some reason, when i check for the input of the spinner using an array with a switch statement it does this weird thing. If i use any sort of "if-statement" "for-loop" any thing of that nature, it wont through an error, just when I open the virtual device it will say app has stopped working. When I remove the if-statement or for-loop it will work fine. The reason I need to use an if statement is because i want to check if the checkbox is checked.

    Heres my code:
    package com.example.gcfcalculator;
     
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.Spinner;
    import android.widget.TextView;
     
    public class MainActivity extends Activity implements OnItemSelectedListener {
     
    	Spinner spinner;
    	String[] paths = {"Rectangle", "Circle", "Triangle"};
    	Button equals;
    	TextView parm1, parm2;
    	EditText value1, value2;
    	CheckBox checkArea, checkPerm;
    	Boolean checkedArea = false, checkedPerm = false;
     
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
    android.R.layout.simple_spinner_item, paths);
            spinner = (Spinner) findViewById(R.id.spinner);
            spinner.setAdapter(adapter);
            spinner.setOnItemSelectedListener(this);
     
        }
     
    	@Override
    	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    		int position = spinner.getSelectedItemPosition();
    		switch(position){
    		case 0:
    			//Rectangle
                            //Problem happens here!!!
    			if(checkArea.isChecked()){
                               System.out.println("Area is checked");
                            }
    			break;
    		case 1:
    			//Square
    			break;
    		case 2:
    			//Triangle
    			break;
    		}
     
    	}
     
    	@Override
    	public void onNothingSelected(AdapterView<?> arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    }


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: CheckBox/Spinner error Error

    The issue is that checkedArea is never instantiated. You will either need to programatically create it or define is by findByView like you did with the spinner.

    checkedArea = (CheckBox)  findViewById(R.id.checkedArea);   //Whatever the id is in the xml layout


    Also, I'd advise using an anonymous class for the listener instead of implementing it at the activity level.

    spinner.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
        {
              switch (position) {
                   //etc
              }
        }
     
     
        @Override
        public void onNothingSelected(AdapterView<?> parentView) {  }
    });

    Also, I'd advise using a string array in the strings.xml instead of defining it in code. IIRC eclipse will complain about defining strings like that.

Similar Threads

  1. Replies: 3
    Last Post: November 30th, 2013, 05:52 PM
  2. Problems with A* Map Search - GC Overload Error and Null Error
    By puneeth.meruva in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2013, 03:01 PM
  3. Replies: 4
    Last Post: October 15th, 2013, 04:33 AM
  4. Please! Help me to this error "ERROR CANNOT FIND SYMBOL"
    By mharck in forum Object Oriented Programming
    Replies: 8
    Last Post: July 3rd, 2012, 09:20 AM
  5. how to delete record based on checkbox selected checkbox
    By -_- in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: December 15th, 2009, 09:26 PM