我知道有很多关于NullPointerException
的问题。我一直在四处寻找答案,但我找不到合适的答案。当我尝试打开energy.xml时它会崩溃,这就是java的问题。谁知道为什么?从Logcat我可以看到问题出在OnCreate
- btnNrj.setOnClickListener(this)
;
我试过清理和重建。
我只能假设我之后会遇到更多问题,但现在最重要的是让它不会崩溃。
public class ActivityEnergy extends Activity implements 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): FATAL EXCEPTION: main 10-27 17:40:58.832: E/AndroidRuntime(1497): java.lang.RuntimeException: Unable to start activity ComponentInfo{se.develope.axson/se.develope.axson.ActivityEnergy}: java.lang.NullPointerException 10-27 17:40:58.832: E/AndroidRuntime(1497): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 10-27 17:40:58.832: E/AndroidRuntime(1497): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 10-27 17:40:58.832: E/AndroidRuntime(1497): at android.app.ActivityThread.access$600(ActivityThread.java:141) 10-27 17:40:58.832: E/AndroidRuntime(1497): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 10-27 17:40:58.832: E/AndroidRuntime(1497): at android.os.Handler.dispatchMessage(Handler.java:99) 10-27 17:40:58.832: E/AndroidRuntime(1497): at android.os.Looper.loop(Looper.java:137) 10-27 17:40:58.832: E/AndroidRuntime(1497): at 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): Caused by: 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): at android.app.Activity.performCreate(Activity.java:5133) 10-27 17:40:58.832: E/AndroidRuntime(1497): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 10-27 17:40:58.832: E/AndroidRuntime(1497): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 10-27 17:40:58.832: E/AndroidRuntime(1497): ... 11 more
答案 0 :(得分:0)
我放弃了为NullPointerException找到解决方案。 相反,我找到了另一种方法来解决没有听众的问题。是@Chronos在另一个线程中提供了解决方案。
在定义按钮广告的XML中
android:onClick="ButtonOnClick"
并在代码中定义“ButtonOnClick”。就我而言,它看起来像这样:
public void ButtonOnClick(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;
你可以在这个主题中找到Chronos答案: Android OnClickListener - identify a button