1

I'm using a button to perform a calculation from an activity (not main) and my app crashes at pressing the button.

My XML is:

<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"/>

And the calculateEngland method is:

public class England_Activity extends 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);
}

}

The logcat shows:

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)
Danielson
  • 2,605
  • 2
  • 28
  • 51
James
  • 102
  • 11
  • 1
    Your log file indicates there is a Null Pointer Exception calling method `getWindowToken()`. Can you confirm that `propValue` is not null when it's call in `hideSoftKeyboard()` – jradich1234 Jul 06 '15 at 20:05
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – njzk2 Jul 06 '15 at 20:12
  • you need to learn about Variable Shadowing. One of the reason a lot of android coding convention states the instance variables to start by `m` – njzk2 Jul 06 '15 at 20:14
  • @njzk2 do you know a good resource to learn about it? – James Jul 06 '15 at 20:15
  • 1
    start by wikipedia. It's a simple concept, really, but knowing about it will help you avoid that sort of problems. https://en.wikipedia.org/wiki/Variable_shadowing – njzk2 Jul 06 '15 at 20:16
  • @njzk2 thanks, I'll check it out – James Jul 06 '15 at 20:19

1 Answers1

1

propValue is declared within your calculateEngland method. Since your code is compiling I assume you've also declared it with class scope, but your statement

EditText propValue = (EditText) findViewById(R.id.propValue);

is only affecting a propValue variable with a scope limited to that calculateEngland method.

Remove the EditText declaration before propValue.

TBridges42
  • 1,849
  • 1
  • 19
  • 29