當我得到當我執行這個代碼的異常:安卓:java.lang.IllegalStateException執行Toast.makeText
public void onClick(View view){
(...)
CharSequence text1 = "Please insert a number.";
int id= view.getId();
(...)
else if(id== R.id.buttonOK){
if(editText.getText().toString().matches(""))
{
Toast.makeText(view.getContext(), text1, Toast.LENGTH_SHORT).show();
}
執行Toast.makeText時,唯一的例外是。我已經嘗試過作爲第一個參數:
getApplicationContext()
這
MyActivity.this
view.getContext()
和我總是得到這個錯誤。你能幫我麼?
例外:
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
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:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
處理這種使用您的activity.this代替「view.getContext()」這一點。 – 2015-03-02 17:03:06
@SurenderKumar沒有工作,我得到了同樣的錯誤 – porthfind 2015-03-02 17:07:20
你確定你是在別的地方犯錯嗎? – Apurva 2015-03-02 17:08:38