我有一个应用程序,其中布局顶部有一个微调器,其下方有一些文本字段,底部有一个名为“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. 到目前为止,我已经为微调器中的每个公式创建了活动。我不知道如何将微调器公式选择与公式活动联系起来。作为初学者,我在这里搜索了所有可能的解决方案,但找不到。谁能帮我一段代码。
我应用了如下所示的解决方案
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
}
});
logcat 显示以下错误
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 代码在这里
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;
}
我添加了这个选线活动
k1_e=(EditText)findViewById(R.id.k1_editText);
k1=Float.parseFloat(k1_e.getText().toString());
更新的 logcat 显示
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
什么问题,我应该如何处理它?