我有一个应用程序,其中布局顶部有一个微调器,下面有一些文本字段,底部有一个名为“RESULT”的按钮。在微调器中,我放置了不同公式的名称,选中它们时必须对文本字段中的数字起作用,当按下RESULT按钮时,必须显示带有结果的新布局。 截至目前,我已经为每个微调器中的公式创建了活动。我不知道如何将微调器公式选择与公式活动相关联。 作为初学者,我在这里搜索了所有可能的解决方案但却找不到它。 任何人都可以帮我一块代码。
我应用了如下所示的解决方案
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
该问题是什么以及我该如何处理它?</ strong>
答案 0 :(得分:1)
截至目前,我已在spinner中为每个公式创建了活动。我不 知道如何将微调器公式选择与公式联系起来 活性。
有一个Class
数组,其中包含每个Activities
的名称(顺序将是Spinner
中出现的公式的顺序):
Class[] clazz = {NameOfActivity1.class, NameOfActivity2.class /*etc*/};
然后,您将拥有一个int
字段( mSelected ),该字段将由OnItemSelectedListener
上的Spinner
更新( mSelected = position )。当显示结果时,只需使用 mSelected 字段以及上面的数组即可开始正确的活动:
Intent i = new Intent(this, clazz[mSelected]);
此外,根据您在结果活动中显示的内容,您可能希望使用单个结果活动来保存包含公式的数组。对于此活动,您将从int
传递当前所选公式的Intent
值(在Spinner
中),您将使用它来构建正确的结果输出。
Selection
活动中的正确实施:
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
}
});