我已在我的应用中实施了应用结算活动。它的工作正常。但是要使购买按钮看不见。我使用了下面的代码,但它不起作用。任何人都可以帮忙。
public class MainActivity extends BlundellActivity implements MainMenu {
public Button topicsbutton;
public Button mediabutton;
public Button purchasetopicsbutton;
public Button purchasemediabutton;
@SuppressWarnings("null")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IInAppBillingService mService = null;
Bundle ownedItems = null;
try {
ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int response = ownedItems.getInt("RESPONSE_CODE");
if (response == 0) {
ArrayList<?> ownedSkus =
ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
ArrayList<?> purchaseDataList =
ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
for (int i = 0; i < purchaseDataList.size(); ++i) {
Object sku = ownedSkus.get(i);
if (sku == Items.SKU1) {
purchasetopicsbutton.setVisibility(View.INVISIBLE);
return;
}
else if (sku == Items.SKU2) {
purchasemediabutton.setVisibility(View.INVISIBLE);
return;
}
}
}
setContentView(R.layout.activity_main);
topicsbutton = (Button) findViewById(R.id.button1a);
mediabutton = (Button) findViewById(R.id.button2a);
purchasetopicsbutton = (Button) findViewById(R.id.button1);
purchasemediabutton = (Button) findViewById(R.id.button2);
}
@Override
public void onTopicsPurchaseItemClick(View v) {
navigate().toPurchaseTopicsActivityForResult();
}
@Override
public void onMediaPurchaseItemClick(View v) {
navigate().toPurchaseMediaActivityForResult();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (Navigator.REQUEST_TOPICS_PURCHASE == requestCode) {
if (RESULT_OK == resultCode) {
dealWithSuccessfulTopicsPurchase();
} else {
dealWithFailedTopicsPurchase();
}
}
else if (Navigator.REQUEST_MEDIA_PURCHASE == requestCode) {
if (RESULT_OK == resultCode) {
dealWithSuccessfulMediaPurchase();
} else {
dealWithFailedMediaPurchase();
}
}
}
public void dealWithSuccessfulTopicsPurchase() {
Log.d("Topics purchased");
popToast("Topics purchased");
topicsbutton.setVisibility(View.VISIBLE);
purchasetopicsbutton.setVisibility(View.INVISIBLE);
}
private void dealWithSuccessfulMediaPurchase() {
Log.d("Media purchased");
popToast("Media purchased");
mediabutton.setVisibility(View.VISIBLE);
purchasemediabutton.setVisibility(View.INVISIBLE);
}
private void dealWithFailedTopicsPurchase() {
Log.d("Topics purchase failed");
popToast("Failed to purchase Topics");
}
private void dealWithFailedMediaPurchase() {
Log.d("Media purchase failed");
popToast("Failed to purchase Media");
}
public void TopicsOpen(View v) {
navigate().toTopicsopen();
}
public void MediaOpen(View v) {
navigate().toMediaopen();
}
}
我在logcat中收到错误
06-28 16:50:25.497: E/AndroidRuntime(10522): FATAL EXCEPTION: main
06-28 16:50:25.497: E/AndroidRuntime(10522): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.firstapp/com.company.firstapp.MainActivity}: java.lang.NullPointerException
06-28 16:50:25.497: E/AndroidRuntime(10522): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-28 16:50:25.497: E/AndroidRuntime(10522): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-28 16:50:25.497: E/AndroidRuntime(10522): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-28 16:50:25.497: E/AndroidRuntime(10522): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-28 16:50:25.497: E/AndroidRuntime(10522): at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 16:50:25.497: E/AndroidRuntime(10522): at android.os.Looper.loop(Looper.java:137)
06-28 16:50:25.497: E/AndroidRuntime(10522): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-28 16:50:25.497: E/AndroidRuntime(10522): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 16:50:25.497: E/AndroidRuntime(10522): at java.lang.reflect.Method.invoke(Method.java:511)
06-28 16:50:25.497: E/AndroidRuntime(10522): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-28 16:50:25.497: E/AndroidRuntime(10522): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-28 16:50:25.497: E/AndroidRuntime(10522): at dalvik.system.NativeStart.main(Native Method)
06-28 16:50:25.497: E/AndroidRuntime(10522): Caused by: java.lang.NullPointerException
06-28 16:50:25.497: E/AndroidRuntime(10522): at com.company.firstapp.MainActivity.onCreate(MainActivity.java:75)
06-28 16:50:25.497: E/AndroidRuntime(10522): at android.app.Activity.performCreate(Activity.java:5104)
06-28 16:50:25.497: E/AndroidRuntime(10522): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-28 16:50:25.497: E/AndroidRuntime(10522): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-28 16:50:25.497: E/AndroidRuntime(10522): ... 11 more
答案 0 :(得分:2)
您将mService设置为null,因此当您尝试使用它时,这将导致NullPointerException(您从日志中看到的错误)。你需要先初始化它。