【问题标题】:IInAppBillingService Object is nullIInAppBillingService 对象为空
【发布时间】: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


【解决方案1】:

您的方法(循环直到mService 为非空)是有缺陷的。事实上,这就是阻止创建服务的原因。

本地“进程内”Service 的创建不会与 bindService()/startService() 调用同步进行。您必须将控制权交还给框架;在这种情况下,这意味着从onStart() 返回。这是因为 Service 的状态由在主线程上运行的消息队列 (Looper) 推进,而当您卡在 onStart() 中时,Looper 不是“循环” .

如果你返回,你会发现 Service.onCreate()ServiceConnection.onServiceConnected() 在几毫秒内被调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-02
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    相关资源
    最近更新 更多