【发布时间】:2019-02-07 22:21:43
【问题描述】:
我正在尝试按照谷歌文档使用 Android In App Billing。但是我尝试使用 bindService 方法来使用 InAppBillingService 对象(mService)。它返回 true,但 mService 仍然为空。这是我的代码
公共类 PaymentActivity 扩展 AppCompatActivity {
IInAppBillingService mService;
ServiceConnection mServiceConn;
ArrayList<String> skuList;
Bundle querySkus;
Bundle skuDetails;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment);
Log.d("payment", "isBillingAvailable? " + isBillingAvailable(this));
String chargeString = getIntent().getStringExtra("charge");
Log.d("intentTest", "charge is: " + chargeString);
mServiceConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d("Payment", "service disconnected!");
mService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
Log.d("Payment", "service connected!");
}
};
}
@Override
protected void onStart() {
super.onStart();
// Bind to IInAppBillingService
Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
this.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
try{
while(mService == null){
Thread.sleep(1000);
Log.d("payment", "sleep 1 second");
}
}catch (InterruptedException e)
{
e.printStackTrace();
}
skuList = new ArrayList<String> ();
skuList.add("premiumUpgrade");
skuList.add("gas");
querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
}
@Override
public void onDestroy() {
super.onDestroy();
if (mService != null) {
unbindService(mServiceConn);
}
}
public static boolean isBillingAvailable(Context context) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
List<ResolveInfo> list = packageManager.queryIntentServices(intent, 0);
return list.size() > 0;
}
}
我知道这是非常糟糕的代码,因为在 onCreate 中调用了 bindService。但我在 Asyn 上试了一下,没有任何变化。我试图通过睡眠 mainTread 等到连接完成。这次尝试使我的应用程序处于无限循环中。
我的错误信息是
java.lang.RuntimeException: 无法启动活动 ComponentInfo{kr.co.bigsapp.www/kr.co.bigsapp.www.activities.PaymentActivity}: java.lang.NullPointerException: 尝试调用接口方法'android. os.Bundle com.android.vending.billing.IInAppBillingService.getSkuDetails(int, java.lang.String, java.lang.String, android.os.Bundle)' 在空对象引用上
请帮我TT
【问题讨论】:
-
注释睡眠循环时出现错误消息
-
发布的代码中没有
bindService()调用。请编辑您的问题。 -
sry.. 我在编辑代码时删除了它
标签: android nullpointerexception in-app-billing