我希望有人可以帮助我吗?我正在尝试解决一个问题,即在公式中输入一定数字。
我做了一些改动,但我仍然无法让它发挥作用。当我尝试打开它时崩溃了。谁知道为什么?从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):at java.lang.reflect.Method.invokeNative(Native Method)
10-27 17:40:58.832:E / AndroidRuntime(1497):at java.lang.reflect.Method.invoke(Method.java:525)
10-27 17:40:58.832:E / AndroidRuntime(1497):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
10-27 17:40:58.832:E / AndroidRuntime(1497):at 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):at 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更多
答案 0 :(得分:0)
尝试调试并检查从微调器获取值。可能是你无法从微调器获得价值。在这种情况下,最好的解决方案是调试。
答案 1 :(得分:0)
问题解决了!终于来了!
首先,最大的问题是SetOnClickListener(THIS); 它让一切都崩溃了。我摆脱了Listener功能,并用以下解决方案取而代之:
在XML文件中 - 添加android:onClick =“ButtonOnClick”
例如:
<Button
android:onClick="ButtonOnClick"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="@string/calcNrjbtn"
android:textSize="8pt"
android:id="@+id/btnCalcNrj">
在java中 - 使用您定义的OnClick替换OnClick
例如:
public void ButtonOnClick(View v) {
// TODO Auto-generated method stub
double num10 = 0;
double num20 = 0;
....
Chronos在以下主题中给了我解决方案:Android OnClickListener - identify a button
由于我使用的是小数,因此INT无效,所以我改为DOUBLE而且一切正常。