我希望有一个人可以帮助我?我正在尝试解决将某个数字放入公式中的问题。
我做了一些改变,但我仍然无法让它工作。当我尝试打开它时它崩溃了。有谁知道为什么?从 Logcat 我可以看到问题出在 OnCreate 中。
公共类 ActivityEnergy 扩展 Activity 实现 OnClickListener {
EditText etNum10;
EditText etNum20;
EditText etNum30;
int nrjspinner = 0;
Object value;
Button btnNrj;
String oper = "";
TextView tvResult;
ArrayList<String> nrjmethod = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.energy);
// find the elements
etNum10 = (EditText) findViewById(R.id.etNum10);
etNum20 = (EditText) findViewById(R.id.etNum20);
etNum30 = (EditText) findViewById(R.id.etNum30);
btnNrj = (Button) findViewById(R.id.btnNrj);
tvResult = (TextView) findViewById(R.id.tvResult);
// set a listener
btnNrj.setOnClickListener(this);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create the ArrayAdapter
ArrayAdapter <CharSequence> adapter = ArrayAdapter.createFromResource( this,
R.array.nrjmethod, android.R.layout.simple_spinner_item);
// Set the Adapter
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
// Set the ClickListener for Spinner
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
value = parent.getItemAtPosition(position);
switch (position) {
case 0:
nrjspinner = 1;
break;
case 1:
nrjspinner = 0.8;
break;
case 2:
nrjspinner = 0.8;
break;
case 3:
nrjspinner = 0.8;
break;
case 4:
nrjspinner = 0.8;
break;
case 5:
nrjspinner = 0.8;
break;
case 6:
nrjspinner = 0.8;
break;
case 7:
nrjspinner = 0.8;
break;
case 8:
nrjspinner = 0.8;
break;
case 9:
nrjspinner = 0.8;
break;
case 10:
nrjspinner = 0.6;
break;
case 11:
nrjspinner = 0.6;
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
float num10 = 0;
float num20 = 0;
float num30 = 0;
float result = 0;
// check if the fields are empty
if (TextUtils.isEmpty(etNum10.getText().toString())
|| TextUtils.isEmpty(etNum20.getText().toString())
|| TextUtils.isEmpty(etNum30.getText().toString())) {
return;
}
// read EditText and fill variables with numbers
num10 = Float.parseFloat(etNum10.getText().toString());
num20 = Float.parseFloat(etNum20.getText().toString());
num30 = Float.parseFloat(etNum30.getText().toString());
// defines the button that has been clicked and performs the corresponding operation
switch (v.getId()) {
case R.id.btnNrj:
oper = "";
result = (float) ((nrjspinner * ((num10 * num20) / num30))*0.001);
break;
default:
break;
}
// form the output line
tvResult.setText("Your result = " + result);
}
}
这是logcat
10-27 17:40:58.832:E/AndroidRuntime(1497):致命异常:主要
10-27 17:40:58.832: E/AndroidRuntime(1497): java.lang.RuntimeException: 无法启动活动 ComponentInfo{se.develope.axson/se.develope.axson.ActivityEnergy}: java.lang.NullPointerException
10-27 17:40:58.832: E/AndroidRuntime(1497): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-27 17:40:58.832: E/AndroidRuntime(1497): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-27 17:40:58.832: E/AndroidRuntime(1497): 在 android.app.ActivityThread.access$600(ActivityThread.java:141)
10-27 17:40:58.832: E/AndroidRuntime(1497): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-27 17:40:58.832: E/AndroidRuntime(1497): 在 android.os.Handler.dispatchMessage(Handler.java:99)
10-27 17:40:58.832: E/AndroidRuntime(1497): 在 android.os.Looper.loop(Looper.java:137)
10-27 17:40:58.832: E/AndroidRuntime(1497): 在 android.app.ActivityThread.main(ActivityThread.java:5103)
10-27 17:40:58.832: E/AndroidRuntime(1497): 在 java.lang.reflect.Method.invokeNative(Native Method)
10-27 17:40:58.832: E/AndroidRuntime(1497): 在 java.lang.reflect.Method.invoke(Method.java:525)
10-27 17:40:58.832: E/AndroidRuntime(1497): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-27 17:40:58.832: E/AndroidRuntime(1497): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-27 17:40:58.832: E/AndroidRuntime(1497): at dalvik.system.NativeStart.main(Native Method)
10-27 17:40:58.832:E/AndroidRuntime(1497):由:java.lang.NullPointerException 引起
10-27 17:40:58.832: E/AndroidRuntime(1497): 在 se.develope.axson.ActivityEnergy.onCreate(ActivityEnergy.java:52)
10-27 17:40:58.832: E/AndroidRuntime(1497): 在 android.app.Activity.performCreate(Activity.java:5133)
10-27 17:40:58.832: E/AndroidRuntime(1497): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-27 17:40:58.832: E/AndroidRuntime(1497): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-27 17:40:58.832: E/AndroidRuntime(1497): ... 11 更多