在我的最新应用上工作,我正在实施应用内购买。
我在网上寻找了很好的教程,并从 Bundell 找到了一个。
我检查了很多次代码,但我不断收到错误消息。看起来像这样:
01-30 21:14:17.415: E/AndroidRuntime(680): FATAL EXCEPTION: main
01-30 21:14:17.415: E/AndroidRuntime(680): java.lang.RuntimeException: Unable to destroy activity {com.crosscommunications.adalert/com.crosscommunications.adalert.inapppurchaseStarter}: java.lang.NullPointerException
01-30 21:14:17.415: E/AndroidRuntime(680): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2672)
01-30 21:14:17.415: E/AndroidRuntime(680): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2690)
01-30 21:14:17.415: E/AndroidRuntime(680): at android.app.ActivityThread.access$2100(ActivityThread.java:117)
01-30 21:14:17.415: E/AndroidRuntime(680): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:964)
01-30 21:14:17.415: E/AndroidRuntime(680): at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 21:14:17.415: E/AndroidRuntime(680): at android.os.Looper.loop(Looper.java:130)
01-30 21:14:17.415: E/AndroidRuntime(680): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-30 21:14:17.415: E/AndroidRuntime(680): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 21:14:17.415: E/AndroidRuntime(680): at java.lang.reflect.Method.invoke(Method.java:507)
01-30 21:14:17.415: E/AndroidRuntime(680): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-30 21:14:17.415: E/AndroidRuntime(680): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-30 21:14:17.415: E/AndroidRuntime(680): at dalvik.system.NativeStart.main(Native Method)
01-30 21:14:17.415: E/AndroidRuntime(680): Caused by: java.lang.NullPointerException
01-30 21:14:17.415: E/AndroidRuntime(680): at android.content.ComponentName.<init>(ComponentName.java:75)
01-30 21:14:17.415: E/AndroidRuntime(680): at android.content.Intent.<init>(Intent.java:2702)
01-30 21:14:17.415: E/AndroidRuntime(680): at com.crosscommunications.adalert.BillingHelper.stopService(BillingHelper.java:261)
01-30 21:14:17.415: E/AndroidRuntime(680): at com.crosscommunications.adalert.inapppurchaseStarter.onDestroy(inapppurchaseStarter.java:129)
01-30 21:14:17.415: E/AndroidRuntime(680): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2659)
01-30 21:14:17.415: E/AndroidRuntime(680): ... 11 more
这是一个 nullpointerException 但我不知道发生了什么。我使用此代码进行应用内购买:
package com.crosscommunications.adalert;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class inapppurchaseStarter extends Activity {
private static final String TAG = "BillingService";
private Context mContext;
private ImageView purchaseableItem;
private Button purchaseButton;
SessionManager session;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("BillingService", "Starting");
setContentView(R.layout.purchasestarter);
mContext = this;
Button annuleren = (Button) findViewById(R.id.purchaseAnnuleren);
Button starter = (Button) findViewById(R.id.koopStarter);
purchaseableItem = (ImageView) findViewById(R.id.gekocht);
startService(new Intent(mContext, BillingService.class));
BillingHelper.setCompletedHandler(mTransactionHandler);
annuleren.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
finish();
}
});
starter.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(BillingHelper.isBillingSupported()){
BillingHelper.requestPurchase(mContext, "android.test.purchased");
// android.test.purchased or android.test.canceled or android.test.refunded or com.blundell.item.passport
} else {
Log.i(TAG,"Can't purchase on this device");
purchaseButton.setEnabled(false); // XXX press button before service started will disable when it shouldnt
}
Log.i(TAG,"default. ID: "+v.getId());
}
});
}
public Handler mTransactionHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
Log.i(TAG, "Transaction complete");
Log.i(TAG, "Transaction status: "+BillingHelper.latestPurchase.purchaseState);
Log.i(TAG, "Item purchased is: "+BillingHelper.latestPurchase.productId);
if(BillingHelper.latestPurchase.isPurchased()){
showItem();
if(BillingHelper.latestPurchase.productId.equals("android.test.purchased")){
System.out.println("Starter is vanaf nu aangekocht");
}
}
};
};
public static Date addMonth(Date date, int i) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, i);
return cal.getTime();
}
private void showItem() {
purchaseableItem.setVisibility(View.VISIBLE);
Toast toast = Toast.makeText(this,
"Product gekocht",
Toast.LENGTH_LONG);
toast.show();
}
@Override
protected void onPause() {
Log.i(TAG, "onPause())");
super.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
BillingHelper.stopService();
}
}
你看我有一个按钮,当点击它会完成活动。每次我完成活动时都会收到此错误....
当 onDestroy 被调用时,事情就变糟了。我认为这与 stopService() 方法有关。
该方法如下所示:
public static void stopService(){
mContext.stopService(new Intent(mContext, BillingService.class));
mService = null;
mContext = null;
mCompletedHandler = null;
Log.i(TAG, "Stopping Service");
}
拜托你们..你能给我提示/指针或只是一个解释吗?
编辑
好的...完成了...我已删除 onDestroy 并将 stopService 复制到完成方法之前...获取另一个 nullpointerexception ..有点相同:
01-30 21:51:07.135: E/AndroidRuntime(741): FATAL EXCEPTION: main
01-30 21:51:07.135: E/AndroidRuntime(741): java.lang.NullPointerException
01-30 21:51:07.135: E/AndroidRuntime(741): at android.content.ComponentName.<init>(ComponentName.java:75)
01-30 21:51:07.135: E/AndroidRuntime(741): at android.content.Intent.<init>(Intent.java:2702)
01-30 21:51:07.135: E/AndroidRuntime(741): at com.crosscommunications.adalert.BillingHelper.stopService(BillingHelper.java:261)
01-30 21:51:07.135: E/AndroidRuntime(741): at com.crosscommunications.adalert.inapppurchaseStarter$2.onClick(inapppurchaseStarter.java:48)
01-30 21:51:07.135: E/AndroidRuntime(741): at android.view.View.performClick(View.java:2485)
01-30 21:51:07.135: E/AndroidRuntime(741): at android.view.View$PerformClick.run(View.java:9080)
01-30 21:51:07.135: E/AndroidRuntime(741): at android.os.Handler.handleCallback(Handler.java:587)
01-30 21:51:07.135: E/AndroidRuntime(741): at android.os.Handler.dispatchMessage(Handler.java:92)
01-30 21:51:07.135: E/AndroidRuntime(741): at android.os.Looper.loop(Looper.java:130)
01-30 21:51:07.135: E/AndroidRuntime(741): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-30 21:51:07.135: E/AndroidRuntime(741): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 21:51:07.135: E/AndroidRuntime(741): at java.lang.reflect.Method.invoke(Method.java:507)
01-30 21:51:07.135: E/AndroidRuntime(741): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-30 21:51:07.135: E/AndroidRuntime(741): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-30 21:51:07.135: E/AndroidRuntime(741): at dalvik.system.NativeStart.main(Native Method)