I have created a OnbuttonClick
function and implement it on two button Calculator and reset.
While clicking on reset its reset the values but while clicking on calculate its make a illegalstatexception.
I have go through the question on this forum but not able to find the solution
package com.example.rajafarid.hc;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class FoodStorage extends MainActivity {
public int sum = 1;
public int result;
public int wheatlb = 25;
public int wheatkg = 10;
public int beanlb = 5;
public int beankg = 2;
EditText editTextNOP;
EditText editTextNOMS;
EditText editTextWheat;
EditText editTextBeans;
Button btnCalculate;
Button btnReset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_storage);
editTextNOP = (EditText) findViewById(R.id.editTextNOP);
editTextNOMS = (EditText) findViewById(R.id.editTextNOMS);
btnCalculate = (Button) findViewById(R.id.btnCalculate);
btnReset = (Button) findViewById(R.id.btnReset);
}
here is the function OnButtonClick
public void OnButtonClick(View v)
{
if (v.getId() == R.id.btnCalculate)
{
result = sum*wheatlb;
int result1 = sum*wheatkg;
editTextWheat.setText(result+"lb or" +wheatkg+"kg");
int bean = sum*beanlb;
int bean1=sum*beankg;
editTextBeans.setText(bean+"lb or"+beankg+"kg");
}
else if (v.getId()==R.id.btnReset)
{
editTextNOP.setText("");
editTextNOMS.setText("");
}
}
}
logcat file:
java.lang.IllegalStateException: Could not execute method of the activity at android.view.View$1.onClick(View.java:4222) at android.view.View.performClick(View.java:5156) at android.view.View$PerformClick.run(View.java:20755) 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:5832) at at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 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:4217) at android.view.View.performClick(View.java:5156) at android.view.View$PerformClick.run(View.java:20755) 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:5832) 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:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference at com.example.rajafarid.hc.FoodStorage.OnButtonClick(FoodStorage.java:42) 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:4217) at android.view.View.performClick(View.java:5156) at android.view.View$PerformClick.run(View.java:20755) 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:5832) 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:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
editTextWheat =null
it's clear – M D