我正在使用按钮从活动(非主要)执行计算,并且按下按钮时我的应用程序崩溃。
我的XML是:
<Button
android:id="@+id/calculate"
android:onClick="calculateEngland"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button"
android:layout_margin="10dp"/>
calculateEngland方法是:
公共类England_Activity扩展了ActionBarActivity {
double x, y;
EditText propValue, stampDuty;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_england);
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
public void calculateEngland(View view)
{
EditText propValue = (EditText) findViewById(R.id.propValue);
EditText stampDuty = (EditText) findViewById(R.id.stampDuty);
hideSoftKeyboard();
x=Double.parseDouble(propValue.getText().toString());
if (x <= 125000 ) {
y = 0.0;
} else if (x <= 250000) {
y = (x - 125000) * 0.02;
} else if (x <= 925000) {
y = (125000 * 0.02) + ((x - 250000) * 0.05);
} else if (x <= 1500000) {
y = (125000 * 0.02) + (675000 * 0.05) + ((x - 925000) * 0.1);
} else if (x > 150000) {
y = (125000 * 0.02) + (675000 * 0.05) + (575000 * 0.1) + ((x - 1500000) * 0.12);
}
stampDuty.setText(Double.toString(y));
}
private void hideSoftKeyboard(){
if(getCurrentFocus()!=null && getCurrentFocus() instanceof EditText){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(propValue.getWindowToken(), 0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_england, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
logcat显示:
Process: uk.xeeleestudios.stampdutycalculator, PID: 3461
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4253)
at android.view.View.performClick(View.java:5197)
at android.view.View$PerformClick.run(View.java:20926)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4248)
at android.view.View.performClick(View.java:5197)
at android.view.View$PerformClick.run(View.java:20926)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.IBinder android.widget.EditText.getWindowToken()' on a null object reference
at uk.xeeleestudios.stampdutycalculator.England_Activity.hideSoftKeyboard(England_Activity.java:57)
at uk.xeeleestudios.stampdutycalculator.England_Activity.calculateEngland(England_Activity.java:35)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4248)
at android.view.View.performClick(View.java:5197)
at android.view.View$PerformClick.run(View.java:20926)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
答案 0 :(得分:1)
propValue
在您的calculateEngland
方法中声明。由于您的代码正在编译,我假设您还使用类范围声明了它,但是您的语句
EditText propValue = (EditText) findViewById(R.id.propValue);
仅影响propValue
变量,其范围仅限于calculateEngland
方法。
删除EditText
之前的propValue
声明。