In-app購入サンプルTrivial Driveを変更していますが、ボタンを追加しましたが、 「onUpgradeAppButtonClicked」メソッドを呼び出す「upgrade_button」(Google Playでプレミアムバージョンを購入したい)java.lang.IllegalStateException:In-app Purchaseを変更すると、アクティビティのメソッドを実行できませんでした。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"だけをクリックします。
問題はどこですか?
非常に判読不能なため、コードを適切にフォーマットしてください... –
'setWaitScreen()'メソッドで探している 'View'の少なくとも1つが' mysterymenubefore'レイアウトに存在しません。 –
[Null Pointer Exceptionとは何か、それを修正する方法は?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do) -i-fix-it) –