views:

116

answers:

1
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

public class calctest extends Activity {
/** Called when the activity is first created. */
private EditText input; 
private EditText input2; 
private EditText input3; 
private EditText input4;
private TextView output;
private TextView output2;
private Spinner aperture;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

 // Extract the text fields from the XML layout 
    input = (EditText) findViewById(R.id.input1); 
    input2 = (EditText) findViewById(R.id.input2);
    input3 = (EditText) findViewById(R.id.input3); 
    input4 = (EditText) findViewById(R.id.input4);
    output = (TextView) findViewById(R.id.result); 
    output2 = (TextView) findViewById(R.id.result2);

    //aperture dropdown
    final Spinner aperture = (Spinner) findViewById(R.id.apt);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(
    this, R.array.apertures, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    aperture.setAdapter(adapter);

    Spinner mode = (Spinner) findViewById(R.id.mode);
    ArrayAdapter adapter2 = ArrayAdapter.createFromResource(
    this, R.array.formats, android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mode.setAdapter(adapter2);

    // Perform calculation when button is pushed

    final Button button = (Button) findViewById(R.id.calculate);
    button.setOnClickListener(new View.OnClickListener() {             
        public void onClick(View v) {                 
            doCalculation(aperture.getSelectedItem());
                }         
            });
        }


private void doCalculation(Object selectedItem) { 
    // Get entered input value 
    String cinput1 = input.getText().toString(); 
    String cinput2 = input2.getText().toString();
    String cinput3 = input3.getText().toString(); 
    String cinput4 = input4.getText().toString();
    Object cinput5 = aperture.getSelectedItem();
}

}

I know that the doCalculation method isnt actually calculating anything yet, because the app crashes just from trying to get the input's value. If I disable the cinput5 line, it works just fine. What am I doing wrong here? And on that note, how do I convert the spinner's value to a string to use in the calculation?

Also, as a double check... the spinner displays a list of numbers (1.4, 2, 2.8, 4, 5.6, etc) and whichever number is selected is the number I need to use in the calculation. I'm calling the correct value from the spinner, right?

I apologize for the probably extremely obvious errors. I just started Java and after I finish this app is promise I will be getting a book!

+2  A: 

You are declaring aperture twice, once at the beginning of your code and again within the onCreate method.

In this line:

final Spinner aperture = (Spinner) findViewById(R.id.apt);

try removing the final Spinner.

When the app reaches the last line of the doCalculation method it is most likely throwing a null object reference because it is referencing the private aperture instance that has yet to be instantiated.

jeremynealbrown
Thanks very much! I'll get the hang of this someday...
Sean