java.lang.IllegalStateException:无法执行方法

时间:2013-07-09 12:07:20

标签: android in-app-purchase illegalstateexception

我在Android方面不是很有经验,而且我已经接管了其他人的工作,并被要求在第3号帐单中实施。

我已经调试了代码,并在此处找到了问题

//IF THE BUTTON IS VISIBLE, AND CLICKED, PURCHASE CALCULATOR  
    public void unlockCalculators(View v) {  
    String payload = "";  
       mHelper.launchPurchaseFlow(this, SKU_riskCalc, RC_REQUEST,  
                mPurchaseFinishedListener, payload);  <---- LINE 250  
            Log.d("BP_EXPERT", "PURCHASE RISK CALC");  

    } 

这是定义mPurchasedListener的地方

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener()  
   {  
       @Override  
       public void onIabPurchaseFinished(IabResult result, Purchase purchase)  
       {  
           Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);  
           if (result.isFailure()) {  
               complain("Error purchasing: " + result);  
           }  
           if (!verifyDeveloperPayload(purchase)) {  
               complain("Error purchasing. Authenticity verification failed.");  
               return;  

           }  
           else if (purchase.getSku().equals(SKU_riskCalc)){  

               //UPDATE SHARED PREFERENCES HERE...  
               SharedPreferences prefs = getSharedPreferences("BloodPressure",MODE_PRIVATE);  
               SharedPreferences.Editor edit = prefs.edit();  
               edit.putBoolean("purchased_calcs", true);  
               edit.commit();  
               Log.d("BP_EXPERT", "PURCHASING RISK CALC");  
               //SELECT CORRECT LAYOUT AFTER PURCHASE  
               createStuff();  
           }  
           else if (purchase.getSku().equals(SKU_all)){  

               //UPDATE SHARED PREFERENCES HERE...  
               SharedPreferences prefs = getSharedPreferences("BloodPressure",MODE_PRIVATE);  
               SharedPreferences.Editor edit = prefs.edit();  
               edit.putBoolean("purchased_all", true);  
               edit.commit();  
               Log.d("BP_EXPERT", "PURCHASING ALL CONTENT");  
               //SELECT CORRECT LAYOUT AFTER PURCHASE  
               createStuff();  
           }  
           Log.d(TAG, "Purchase successful.");  
       }  
   };  

当应用程序进入启动购买流程时,它被强制关闭。如果有人对可能出现的问题有任何想法,我会非常感激。 我遇到过这个问题,我不太确定是什么导致它,当我点击按钮时我在logcat中得到以下内容

7-09 11:48:14.495: E/AndroidRuntime(1702): FATAL EXCEPTION: main  
07-09 11:48:14.495: E/AndroidRuntime(1702): java.lang.IllegalStateException: Could not execute method of the activity  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at android.view.View$1.onClick(View.java:2072)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at android.view.View.performClick(View.java:2408)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at android.view.View$PerformClick.run(View.java:8817)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at android.os.Handler.handleCallback(Handler.java:587)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at android.os.Handler.dispatchMessage(Handler.java:92)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at android.os.Looper.loop(Looper.java:144)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at android.app.ActivityThread.main(ActivityThread.java:4937)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at java.lang.reflect.Method.invokeNative(Native Method)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at java.lang.reflect.Method.invoke(Method.java:521)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at dalvik.system.NativeStart.main(Native Method)  
07-09 11:48:14.495: E/AndroidRuntime(1702): Caused by: java.lang.reflect.InvocationTargetException  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at com.bpxpert.bloodpressure.CalculatorRisk.unlockCalculators(CalculatorRisk.java:250)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at java.lang.reflect.Method.invokeNative(Native Method)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at java.lang.reflect.Method.invoke(Method.java:521)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at android.view.View$1.onClick(View.java:2067)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     ... 11 more  
07-09 11:48:14.495: E/AndroidRuntime(1702): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@462b6c10 is not valid; is your activity running?  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at android.view.ViewRoot.setView(ViewRoot.java:509)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at android.view.Window$LocalWindowManager.addView(Window.java:424)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at android.app.Dialog.show(Dialog.java:241)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at com.bpxpert.bloodpressure.CalculatorRisk.alert(CalculatorRisk.java:382)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at com.bpxpert.bloodpressure.CalculatorRisk.complain(CalculatorRisk.java:374)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at com.bpxpert.bloodpressure.CalculatorRisk$3.onIabPurchaseFinished(CalculatorRisk.java:277)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at com.bpxpert.bloodpressure.util.IabHelper.launchPurchaseFlow(IabHelper.java:376)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     at com.bpxpert.bloodpressure.util.IabHelper.launchPurchaseFlow(IabHelper.java:324)  
07-09 11:48:14.495: E/AndroidRuntime(1702):     ... 15 more  
07-09 11:48:14.505: W/ActivityManager(105):   Force finishing activity com.bpxpert.bloodpressure/.BloodPressureActivity  

如果有人能指出我出错的地方,我真的很感激。

1 个答案:

答案 0 :(得分:2)

问题出在这里

  

07-09 11:48:14.495:E / AndroidRuntime(1702):引起:android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.app.LocalActivityManager $ LocalActivityRecord@462b6c10无效;你的活动在运行吗?
  07-09 11:48:14.495:E / AndroidRuntime(1702):在android.view.ViewRoot.setView(ViewRoot.java:509)
  07-09 11:48:14.495:E / AndroidRuntime(1702):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
  07-09 11:48:14.495:E / AndroidRuntime(1702):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
  07-09 11:48:14.495:E / AndroidRuntime(1702):在android.view.Window $ LocalWindowManager.addView(Window.java:424)
  07-09 11:48:14.495:E / AndroidRuntime(1702):在android.app.Dialog.show(Dialog.java:241)
   07-09 11:48:14.495:E / AndroidRuntime(1702):at com.bpxpert.bloodpressure.CalculatorRisk.alert(CalculatorRisk.java:382)

我确信下面的一行会尝试显示一个对话框

mHelper.launchPurchaseFlow(this, SKU_riskCalc, RC_REQUEST,  
            mPurchaseFinishedListener, payload);

但某个地方你的活动被破坏了,因此对话框无法显示,应用程序崩溃。

修改 在您的类中声明一个Activity

类型的变量
private Activity mActivityContext = null;
OnCreate方法中的

初始化变量

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mActivityContext = this;

    // Your Rest of the code
}

然后在调用launchPurchaseFlow时使用变量而不是“ this

mHelper.launchPurchaseFlow(mActivityContext, SKU_riskCalc, RC_REQUEST,  
            mPurchaseFinishedListener, payload);