2013-07-09 162 views
0

我對Android並不是很有經驗,我已經接管別人的工作,並被要求在計費v3中實施。java.lang.IllegalStateException:無法執行方法

我已經調試代碼,並已經找到這裏的問題

//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 [email protected] 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 

如果有人能指出我在哪裏」我錯了我真的很感激它。

回答

2

的問題是在這裏

十一月七日至九日:48:14.495:E/AndroidRuntime(1702):android.view.WindowManager $ BadTokenException:由造成無法添加窗口 - 標記機器人。 [email protected]無效;你的活動正在運行?
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)

我相信下面的線設法顯示一個對話框

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

但是某處將您的活動被破壞掉了,所以對話框不能顯示和應用程序崩潰。

編輯: 在你的類在你onCreate方法聲明類型活動

private Activity mActivityContext = null; 

的變量初始化變量

@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); 
+0

謝謝@Rajeev回到我身邊。第382行是警報方法'void alert(String message)的最後一行{AlertDialog.Builder bld = new AlertDialog。助洗劑(本); bld.setMessage(message); bld.setNeutralButton(「OK」,null); Log.d(TAG,「顯示警告對話框:」+消息); Log.d bld.create()。show(); }」,但我也有在這些採購等活動這個功能,但沒有錯誤 –

+0

當我踏進aunchPurchaseFlow方法,我也是在警報系統設置一個斷點** bld.create()。節目() ; **這行似乎是一個問題, –

+0

AlertDialog.Builder bld = new AlertDialog.Builder(this);會將新對話框關聯到正在創建對話框的活動。當你在做bld.create.show()時,創建這個對話框的活動(== this)已經被銷燬了。檢查在該活動中調用的任何finish()方法。 – Rajeev

相關問題