human
human

Reputation: 647

How to connect spinner item selection to an activity?

I have an app where there is a spinner at the top of the layout and some text fields below it and a button at the bottom named "RESULT". In the spinner I have put name of different formulas which when selected must act upon the numbers in the text field and when the RESULT button is pressed a new layout with the result must be displayed. As of now I have created activity for each formula in spinner. I don't know how to connect spinner formula selection with the formula activity. Being a beginner I have searched for every possible solution here but couldn't find it. Can anyone help me with a piece of code.


I applied the solution as shown below

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.selective);
final Class[] clazz={Srkt.class,Binkhorst.class,Srk2.class,Holladay.class};
spin=(Spinner)findViewById(R.id.formulae);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long id) {
        // TODO Auto-generated method stub
        Intent i=new Intent(Selection.this,clazz[pos]);
        startActivity(i);

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
    });

the logcat shows following errors

    12-28 15:17:41.336: E/Trace(768): error opening trace file: No such file or directory (2)
12-28 15:17:44.554: E/AndroidRuntime(768): FATAL EXCEPTION: main
12-28 15:17:44.554: E/AndroidRuntime(768): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.iolcalci/com.example.iolcalci.Srkt}: java.lang.NullPointerException
12-28 15:17:44.554: E/AndroidRuntime(768):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-28 15:17:44.554: E/AndroidRuntime(768):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-28 15:17:44.554: E/AndroidRuntime(768):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-28 15:17:44.554: E/AndroidRuntime(768):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-28 15:17:44.554: E/AndroidRuntime(768):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 15:17:44.554: E/AndroidRuntime(768):  at android.os.Looper.loop(Looper.java:137)
12-28 15:17:44.554: E/AndroidRuntime(768):  at android.app.ActivityThread.main(ActivityThread.java:5039)
12-28 15:17:44.554: E/AndroidRuntime(768):  at java.lang.reflect.Method.invokeNative(Native Method)
12-28 15:17:44.554: E/AndroidRuntime(768):  at java.lang.reflect.Method.invoke(Method.java:511)
12-28 15:17:44.554: E/AndroidRuntime(768):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-28 15:17:44.554: E/AndroidRuntime(768):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-28 15:17:44.554: E/AndroidRuntime(768):  at dalvik.system.NativeStart.main(Native Method)
12-28 15:17:44.554: E/AndroidRuntime(768): Caused by: java.lang.NullPointerException
12-28 15:17:44.554: E/AndroidRuntime(768):  at com.example.iolcalci.Srkt.onCreate(Srkt.java:34)
12-28 15:17:44.554: E/AndroidRuntime(768):  at android.app.Activity.performCreate(Activity.java:5104)
12-28 15:17:44.554: E/AndroidRuntime(768):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-28 15:17:44.554: E/AndroidRuntime(768):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-28 15:17:44.554: E/AndroidRuntime(768):  ... 11 more

Srkt code here

public class Srkt extends Activity{
EditText k1_e,k2_e,al_e,alconst_e;
float k1,k2,al,al_const;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
     float Rcor;
     float Lcor;
     float Crwdest;
     float Avg_k;
     float Corneal_H;
     float Acd_Const;
     float Offset;
     float Acd_Est;
     float Na=(float) 1.34;
     float C2=(float) 0.33;
     float C3;
     float C4;
     float C5;
     float C6;
     float C8;
     float C9;
     float Iolam;


    k1_e=(EditText)findViewById(R.id.k1_editText);
    k1=Float.parseFloat(k1_e.getText().toString());
    k2_e=(EditText)findViewById(R.id.k2_editText);
    k2=Float.parseFloat(k2_e.getText().toString());
    al_e=(EditText)findViewById(R.id.al_editText);//line 37
    al=Float.parseFloat(al_e.getText().toString());
    alconst_e=(EditText)findViewById(R.id.al_const_editText);
    al_const=Float.parseFloat(alconst_e.getText().toString()); 


    Avg_k=(k1+k2)/2;
    float Rcor1=(float)(337.5/Avg_k);Rcor=Round(Rcor1,2);

    if(al<=24.2){
        Lcor=al;
    }else{
        Lcor=(float) (-3.446+1.716*al-0.0237*(al*al));
    }Lcor=Round(Lcor,2);
    Crwdest=(float) (-5.41+0.58412*Lcor+0.098*Avg_k);Crwdest=Round(Crwdest,2);
    Corneal_H=(float) (Rcor-(Math.sqrt(Rcor*Rcor-Crwdest*Crwdest/4)));Corneal_H=Round(Corneal_H,2);
    Acd_Const=(float) (0.62467*al_const-68.747);
    Offset=(float) (Acd_Const-3.336);Offset=Round(Offset,2);
    Acd_Est=(Corneal_H+Offset); float Acd_Est1=Round(Acd_Est,2);
    C3=(float) (0.97971*al+0.65696);C3=Round(C3,2);     
    C4=C3-Acd_Est1;C4=Round(C4,2);
    C5=(float) ((Na*Rcor)-(C2*Acd_Est)); C5=Round(C5, 2);
    C6=(float) ((Na*Rcor1)-(C2*C3));
    C8=(float) ((12*C6)+(C3*Rcor1));C8=Round(C8,2);
    C9=(float) ((12*C5)+(Acd_Est*Rcor1));C9=Round(C9,2);
    Iolam=(float) ((1336*(C6-(0.001*C8*0)))/(C4*(C5-(0.001*0*C9))));Iolam=Round(Iolam,2);                       /*Desired Refraction not taken*/
    }

    public static float Round(float Rval, int Rpl) {
          float p = (float)Math.pow(10,Rpl);
          Rval = Rval * p;
          float tmp = Math.round(Rval);
          return (float)tmp/p;
          }

I have added this line selection activity

k1_e=(EditText)findViewById(R.id.k1_editText);
k1=Float.parseFloat(k1_e.getText().toString());

updated logcat shows

    12-29 07:16:54.642: E/AndroidRuntime(766): FATAL EXCEPTION: main
12-29 07:16:54.642: E/AndroidRuntime(766): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.iolcalci/com.example.iolcalci.Selection}: java.lang.NumberFormatException: Invalid float: ""
12-29 07:16:54.642: E/AndroidRuntime(766):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-29 07:16:54.642: E/AndroidRuntime(766):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-29 07:16:54.642: E/AndroidRuntime(766):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-29 07:16:54.642: E/AndroidRuntime(766):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-29 07:16:54.642: E/AndroidRuntime(766):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 07:16:54.642: E/AndroidRuntime(766):  at android.os.Looper.loop(Looper.java:137)
12-29 07:16:54.642: E/AndroidRuntime(766):  at android.app.ActivityThread.main(ActivityThread.java:5039)
12-29 07:16:54.642: E/AndroidRuntime(766):  at java.lang.reflect.Method.invokeNative(Native Method)
12-29 07:16:54.642: E/AndroidRuntime(766):  at java.lang.reflect.Method.invoke(Method.java:511)
12-29 07:16:54.642: E/AndroidRuntime(766):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-29 07:16:54.642: E/AndroidRuntime(766):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-29 07:16:54.642: E/AndroidRuntime(766):  at dalvik.system.NativeStart.main(Native Method)
12-29 07:16:54.642: E/AndroidRuntime(766): Caused by: java.lang.NumberFormatException: Invalid float: ""
12-29 07:16:54.642: E/AndroidRuntime(766):  at java.lang.StringToReal.invalidReal(StringToReal.java:63)
12-29 07:16:54.642: E/AndroidRuntime(766):  at java.lang.StringToReal.parseFloat(StringToReal.java:289)
12-29 07:16:54.642: E/AndroidRuntime(766):  at java.lang.Float.parseFloat(Float.java:300)
12-29 07:16:54.642: E/AndroidRuntime(766):  at com.example.iolcalci.Selection.onCreate(Selection.java:25)
12-29 07:16:54.642: E/AndroidRuntime(766):  at android.app.Activity.performCreate(Activity.java:5104)
12-29 07:16:54.642: E/AndroidRuntime(766):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-29 07:16:54.642: E/AndroidRuntime(766):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-29 07:16:54.642: E/AndroidRuntime(766):  ... 11 more

whats the problem and how how am i supposed to deal with it?

Upvotes: 0

Views: 838

Answers (1)

user
user

Reputation: 87064

As of now I have created activity for each formula in spinner. I don't know how to connect spinner formula selection with the formula activity.

Have an array of Class holding the name of each of those Activities(the order will be the order of the formulas as they appear in the Spinner):

Class[] clazz = {NameOfActivity1.class, NameOfActivity2.class /*etc*/};

You'll then have a int field(mSelected) which will be updated by the OnItemSelectedListener on the Spinner(mSelected = position). When it's time to show the results just use the mSelected field along with the array above to start the proper activity:

Intent i = new Intent(this, clazz[mSelected]);

Also, depending on what you show in the results activities, you may want to have a single results activity which will hold an array with the formulas. To this activity you'll pass an int value(in the Intent) of the currently selected formula from the Spinner and you'll use that to build the proper result output.

Proper implementation in the Selection activity:

private int mSelected = -1;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selective);
final Class[] clazz={Srkt.class,Binkhorst.class,Srk2.class,Holladay.class};
spin=(Spinner)findViewById(R.id.formulae);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long id) {
        mSelected = pos;
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
       mSelected = -1;
    }
    });
// find the results Button and set a OnClickListener where you'll do the calculation
Button resultsButton = (Button) findViewById(R.id.the_id_of_the_results_button);
resultsButton.setOnClickListener(new OnCLickListener() {

    @Override
    public void onClick(View v) {
         // In here you'll do the calculation
         // you know the selected formula from the value of mSelected
         // after you do the calculation you will start the proper results activity using the clazz array and mSelected
        // you would also pass the results you calculated in the Intent used to start the new activity
    }

});

Upvotes: 1

Related Questions