2013-01-18 3 views
2

나는 최근에 내가 작업하고있는 게임에서 앱 청구서를 구현하려고 노력했지만 NullPointerException을 얻었고 그 이유를 알 수 없다.인앱 결제로 작업 할 때 NullPointerException이 발생합니까?

BuyActivity.java :

package com.liamw.games.whatami; 

import java.util.ArrayList; 
import java.util.List; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.provider.Settings.Secure; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 

import com.liamw.games.whatami.util.IabHelper; 
import com.liamw.games.whatami.util.IabResult; 
import com.liamw.games.whatami.util.Inventory; 
import com.liamw.games.whatami.util.Purchase; 

public class BuyActivity extends Activity { 

    String key, androidid; 
    IabHelper mHelper; 
    Button fifty; 
    ProgressDialog wait; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // DONE Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_buy); 

     fifty = (Button) findViewById(R.id.b50hints); 
     androidid = Secure.getString(BuyActivity.this.getContentResolver(), 
       Secure.ANDROID_ID); 

     final ProgressDialog wait = new ProgressDialog(this); 
     wait.setTitle("Updating..."); 
     wait.setMessage("Please wait while the details are loaded..."); 
     wait.setCancelable(false); 
     wait.show(); 

     key = "xxxxx"; 
     mHelper = new IabHelper(this, key); 

     mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { 
      public void onIabSetupFinished(IabResult result) { 
       if (!result.isSuccess()) { 
        // Oh noes, there was a problem. 
        Log.d("What Am I - IAB", 
          "Problem setting up In-app Billing: " + result); 
       } 
       // Hooray, IAB is fully set up! 

       List<String> additionalSkuList = new ArrayList<String>(); 
       additionalSkuList.add("50hints"); 

       mHelper.queryInventoryAsync(true, additionalSkuList, 
         mQueryFinishedListener); 

      } 
     }); 

     fifty.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 

       mHelper.launchPurchaseFlow(BuyActivity.this, "50hints", 10001, 
         mPurchaseFinishedListener, androidid); 
       fifty.setEnabled(false); 

      } 
     }); 

    } 

    IabHelper.QueryInventoryFinishedListener mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener() { 
     public void onQueryInventoryFinished(IabResult result, 
       Inventory inventory) { 
      if (result.isFailure()) { 
       // handle error 
       return; 
      } 

      String fiftyhintscost = inventory.getSkuDetails("50hints").getPrice(); 

      fifty.setText("50 Hints: " + fiftyhintscost); 
      wait.dismiss(); 
      // update the UI 
     } 
    }; 

    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() { 
     public void onIabPurchaseFinished(IabResult result, Purchase purchase) { 
      if (result.isFailure()) { 
       Log.d("What Am I? IAB", "Error purchasing: " + result); 
       return; 
      } else if (purchase.getSku().equals("50hints") 
        && purchase.getDeveloperPayload().equals(androidid)) { 
       // consume the gas and update the UI 
       mHelper.consumeAsync(purchase, mConsumeFinishedListener); 
      } 
      fifty.setEnabled(true); 
     } 
    }; 

    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() { 
     public void onConsumeFinished(Purchase purchase, IabResult result) { 
      if (result.isSuccess()) { 
       // provision the in-app purchase to the user 
       // (for example, credit 50 gold coins to player's character) 
       Toast.makeText(BuyActivity.this, 
         "Purchase Successful - Crediting!", Toast.LENGTH_LONG) 
         .show(); 
      } else { 
       // handle error 
      } 
     } 
    }; 

    @Override 
    public void onDestroy() { 
     if (mHelper != null) 
      mHelper.dispose(); 
     mHelper = null; 
     super.onDestroy(); 
    } 
} 

로그 캣 :

01-18 19:12:45.829: E/AndroidRuntime(16849): FATAL EXCEPTION: main 
01-18 19:12:45.829: E/AndroidRuntime(16849): java.lang.NullPointerException 
01-18 19:12:45.829: E/AndroidRuntime(16849): at com.liamw.games.whatami.BuyActivity$1.onQueryInventoryFinished(BuyActivity.java:87) 
01-18 19:12:45.829: E/AndroidRuntime(16849): at com.liamw.games.whatami.util.IabHelper$2$1.run(IabHelper.java:533) 
01-18 19:12:45.829: E/AndroidRuntime(16849): at android.os.Handler.handleCallback(Handler.java:725) 
01-18 19:12:45.829: E/AndroidRuntime(16849): at android.os.Handler.dispatchMessage(Handler.java:92) 
01-18 19:12:45.829: E/AndroidRuntime(16849): at android.os.Looper.loop(Looper.java:137) 
01-18 19:12:45.829: E/AndroidRuntime(16849): at android.app.ActivityThread.main(ActivityThread.java:5039) 
01-18 19:12:45.829: E/AndroidRuntime(16849): at java.lang.reflect.Method.invokeNative(Native Method) 
01-18 19:12:45.829: E/AndroidRuntime(16849): at java.lang.reflect.Method.invoke(Method.java:511) 
01-18 19:12:45.829: E/AndroidRuntime(16849): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
01-18 19:12:45.829: E/AndroidRuntime(16849): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
01-18 19:12:45.829: E/AndroidRuntime(16849): at dalvik.system.NativeStart.main(Native Method) 

행 87 :

String fiftyhintscost = inventory.getSkuDetails("50hints").getPrice(); 

나를 NullPointer의 원인을 찾을 수 있도록하고 해결 방법을하는 방법을 가르쳐주세요 ...

+1

'inventory' 또는'getSkuDetails()'의 결과가 null 인 경우, 그 밖의 무엇이 잘못되었을 수 있습니까? – dmon

+0

그래,하지만 @dmon을 어떻게 수정해야합니까? 그것은 null이되어서는 안된다? –

+0

나는 무엇이 잘못 될 수 있는지 모른다. 그게 바로 스키 야? 디버그를 통해 재고의 값을 확인하십시오. 그리고지도가 나타나는 것으로부터 무언가를 검색 할 때 특히 아무것도 아닌 것으로 가정하지 마십시오. – dmon

답변

1

턴 SKU에서 문자 만 사용할 수 있다는 사실을 알았습니다.

+3

이것이 사실이라고 생각하지 않습니다. 특히 문서에서 : 제품 ID는 응용 프로그램의 네임 스페이스에서 고유합니다. 제품 ID는 소문자 또는 숫자로 시작해야하며 소문자 (a-z), 숫자 (0-9), 밑줄 (_) 및 점 (.) 만 사용하여 작성해야합니다. 제품 ID "android.test"는 "android.test"로 시작하는 모든 제품 ID와 마찬가지로 예약되어 있습니다. 또한 항목의 제품 ID를 만든 후에 수정할 수 없으며 제품 ID를 다시 사용할 수 없습니다. http://developer.android.com/google/play/billing/billing_admin.html – sghael

관련 문제