0
我正在修改应用内购买示例Trivial Drive,添加一些按钮但出现错误,单击“ upgrade_button“,它调用了”onUpgradeAppButtonClicked“方法(我想去谷歌播放高级版本)。java.lang.IllegalStateException:在修改应用内购买TrivialDrive示例时无法执行该活动的方法
日志堆栈跟踪是:
FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3854)
at android.view.View.performClick(View.java:4469)
at android.view.View$PerformClick.run(View.java:18788)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5349)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:835)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3849)
... 11 more
Caused by: java.lang.NullPointerException
at com.lm.rosary.Mysterytoprayfirstbefore.setWaitScreen(Mysterytoprayfirstbefore.java:511)
at com.lm.rosary.Mysterytoprayfirstbefore.onUpgradeAppButtonClicked(Mysterytoprayfirstbefore.java:364)
... 14 more
我的Java程序,点击 “upgrade_button” 是:
> public class Mysterytoprayfirstbefore extends Activity implements
> IabBroadcastListener,
> OnClickListener {
> Button btnJoyful;
> Button btnSorrowful;
> Button btnGlorious;
> Button btnLuminous; Button btnAutoselectmystery; Button
> btnBacktoMainMenu; Button upgrade_button;
> `SharedPreferences prefs;`
> SharedPreferences.Editor editor;
>
> static final String TAG = "BuyPremium";
>
> // Does the user have the premium upgrade?
>
> boolean mIsPremium = false;
>
>
> // SKUs for our products: the premium upgrade (non-consumable) and gas (consumable)
>
> static final String SKU_PREMIUM = "xxxxxx001";
>
> // (arbitrary) request code for the purchase flow
>
> static final int RC_REQUEST = 10001;
>
> // The helper object
>
> IabHelper mHelper;
>
> // Provides purchase notification while this app is running
>
> IabBroadcastReceiver mBroadcastReceiver;
>
> @Override
> public void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
> setContentView(R.layout.mysterymenubefore);
>
>
> String base64EncodedPublicKey = "xxxxxxxQIDAQAB";
>
> // Create the helper, passing it our context and the public key to verify signatures with
> Log.d(TAG, "Creating IAB helper.");
>
> mHelper = new IabHelper(this, base64EncodedPublicKey);
>
> // enable debug logging (for a production application, you should set this to false).
>
> mHelper.enableDebugLogging(false);
>
> // Start setup. This is asynchronous and the specified listener
> // will be called once setup completes.
>
> Log.d(TAG, "Starting setup.");
>
> mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
> public void onIabSetupFinished(IabResult result) {
>
> Log.d(TAG, "Setup finished.");
>
> if (!result.isSuccess()) {
>
> return;
> }
>
> // Have we been disposed of in the meantime? If so, quit.
>
> if (mHelper == null) return;
>
>
>
> mBroadcastReceiver = new IabBroadcastReceiver(Mysterytoprayfirstbefore.this);
> IntentFilter broadcastFilter = new IntentFilter(IabBroadcastReceiver.ACTION);
> registerReceiver(mBroadcastReceiver, broadcastFilter);
>
> // IAB is fully set up. Now, let's get an inventory of stuff we own.
> Log.d(TAG, "Setup successful. Querying inventory.");
>
> mHelper.queryInventoryAsync(mGotInventoryListener);
> }
> });
>
> btnJoyful = (Button) findViewById(R.id.btnJoyful); btnSorrowful = (Button) findViewById(R.id.btnSorrowful);
> btnGlorious = (Button) findViewById(R.id.btnGlorious); btnLuminous = (Button) findViewById(R.id.btnLuminous);
> btnAutoselectmystery = Button)findViewById(R.id.btnAutoselectmystery);
> ` btnBacktoMainMenu =(Button) findViewById(R.id.btnBacktoMainMenu);
>
> upgrade_button = (Button) findViewById(R.id.upgrade_button);
>
>
> btnJoyful.setOnClickListener(new View.OnClickListener() {
> @Override public void onClick(View view) {
> // Launching All products Activity
> Intent i = new Intent(getApplicationContext(), Jfp1.class);
> startActivity(i); } });
>
> // view products click event btnSorrowful.setOnClickListener(new View.OnClickListener() {
> @Override public void onClick(View view) {
> // Launching create new product activity
> Intent i = new Intent(getApplicationContext(), Sfp1.class);
> startActivity(i);
> } });
> btnLuminous.setOnClickListener(new View.OnClickListener() {
>
> @Override
> public void onClick(View view) {
> // Launching create new product activity
> Intent i = new Intent(getApplicationContext(), Mysterytoprayfirstbefore.class);
> startActivity(i);
>
> } });
> btnGlorious.setOnClickListener(new View.OnClickListener() {
> @Override
> public void onClick(View view) {
> Intent i = new Intent(getApplicationContext(), Mysterytoprayfirstbefore.class);
> startActivity(i);
> } });
> btnAutoselectmystery.setOnClickListener(new View.OnClickListener() {
>
> @Override
> public void onClick(View view) {
> Intent i = new Intent(getApplicationContext(), Mysterytoprayfirstbefore.class);
> startActivity(i);
> } });
> btnBacktoMainMenu.setOnClickListener(new View.OnClickListener() {
> @Override
> public void onClick(View view) {
>
> Intent i = new Intent(getApplicationContext(), MainActivity.class);
> startActivity(i);
> } }); }
>
>
> IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
> public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
>
> Log.d(TAG, "Query inventory finished.");
>
> if (mHelper == null) return;
>
>
> if (result.isFailure()) {
> complain("Failed to query inventory: " + result);
> return;
> }
>
> Log.d(TAG, "Query inventory was successful.");
>
> // Do we have the premium upgrade?
>
> Purchase premiumPurchase = inventory.getPurchase(SKU_PREMIUM);
> mIsPremium = (premiumPurchase != null && verifyDeveloperPayload(premiumPurchase));
>
> Log.d(TAG, "User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"));
>
>
> Log.d(TAG, "Initial inventory query finished; enabling main UI.");
>
> }
> };
>
> @Override
> public void receivedBroadcast() {
> // Received a broadcast notification that the inventory of items has changed
> Log.d(TAG, "Received broadcast notification. Querying inventory.");
>
> mHelper.queryInventoryAsync(mGotInventoryListener);
> }
>
> // User clicked the "Upgrade to Premium" button.
>
> public void onUpgradeAppButtonClicked(View arg0) {
>
> Log.d(TAG, "Upgrade button clicked; launching purchase flow for upgrade.");
>
> setWaitScreen(true);
>
> String payload = "";
>
> mHelper.launchPurchaseFlow(this, SKU_PREMIUM, RC_REQUEST,
> mPurchaseFinishedListener, payload);
> }
>
>
>
> @Override
> protected void onActivityResult(int requestCode, int resultCode, Intent data) {
> Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
> if (mHelper == null) return;
>
> // Pass on the activity result to the helper for handling
>
> if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
>
> super.onActivityResult(requestCode, resultCode, data);
> }
> else {
> Log.d(TAG, "onActivityResult handled by IABUtil.");
> }
> }
>
> /** Verifies the developer payload of a purchase. */
>
> boolean verifyDeveloperPayload(Purchase p) {
> String payload = p.getDeveloperPayload();
> return true;
> }
>
> // Callback for when a purchase is finished
>
> IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
> public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
>
> Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);
>
> // if we were disposed of in the meantime, quit.
>
> if (mHelper == null) return;
>
> if (result.isFailure()) {
> complain("Error purchasing: " + result);
> setWaitScreen(false);
> return;
> }
> if (!verifyDeveloperPayload(purchase)) {
> complain("Error purchasing. Authenticity verification failed.");
> setWaitScreen(false);
> return;
> }
>
> Log.d(TAG, "Purchase successful.");
>
> if (purchase.getSku().equals(SKU_PREMIUM)) {
>
> // bought the premium upgrade!
>
> Log.d(TAG, "Purchase is premium upgrade. Congratulating user.");
>
> alert("Thank you for upgrading to premium!");
>
> mIsPremium = true;
>
> Intent i = new Intent(getApplicationContext(), Mysterytopray.class);
> startActivity(i);
>
> setWaitScreen(false);
> }
>
> }
> };
>
>
>
>
> // We're being destroyed. It's important to dispose of the helper here!
> @Override
> public void onDestroy() {
> super.onDestroy();
>
> // very important:
> if (mBroadcastReceiver != null) {
> unregisterReceiver(mBroadcastReceiver);
> }
>
> // very important:
> Log.d(TAG, "Destroying helper.");
>
> if (mHelper != null) {
> mHelper.dispose();
> mHelper = null;
> }
> }
>
> // Enables or disables the "please wait" screen.
>
> void setWaitScreen(boolean set) {
> findViewById(R.id.screen_main).setVisibility(set ? View.GONE : View.VISIBLE);
> findViewById(R.id.screen_wait).setVisibility(set ? View.VISIBLE : View.GONE);
> }
>
> void complain(String message) {
>
> Log.e(TAG, "**** BuyPremium Error: " + message);
>
> alert("Error: " + message);
> }
>
> void alert(String message) {
> AlertDialog.Builder bld = new AlertDialog.Builder(this);
> bld.setMessage(message);
> bld.setNeutralButton("OK", null);
> Log.d(TAG, "Showing alert dialog: " + message);
> bld.create().show();
> }
>
>
>
> @Override public void onClick(DialogInterface arg0, int arg1) { //
> TODO Auto-generated method stub
> }
而对于upgrade_button屏幕下方的xml:
> <Button android:id="@+id/upgrade_button"
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> android:text="@string/button_textbuypremium"
> android:onClick="onUpgradeAppButtonClicked"
> style="@style/BlackText"
> android:background="@color/orange"
>
> android:layout_marginTop="25dip"/>
其他按钮,当按下/单击它时没问题,只有发出IlegalSateException的“upgrade_button”。
问题在哪里?
请正确格式化您的代码,因为它非常难以读取...... –
在'mysterymenubefore'布局中不存在您在'setWaitScreen()'方法中查找的'View'中的至少一个。 –
可能的重复[什么是空指针异常,以及如何解决它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do -i-fix-it) –