Android 从类启动方法会导致空指针异常

Android 从类启动方法会导致空指针异常,android,nullpointerexception,in-app-purchase,Android,Nullpointerexception,In App Purchase,我试图从我的类访问/调用活动中的方法,但这样做时会出现空指针异常 这是我试图调用的方法。此方法在我的活动中: public void start(Context context){ mHelper = new IabHelper(context.getApplicationContext(), base64EncodedPublicKey); mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {

我试图从我的类访问/调用活动中的方法,但这样做时会出现空指针异常

这是我试图调用的方法。此方法在我的活动中:

    public void start(Context context){

    mHelper = new IabHelper(context.getApplicationContext(), base64EncodedPublicKey);

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                Log.d(TAG, "In-app Billing setup failed: " + result);

            } else {
                Log.d(TAG, "In-app Billing is set up OK");

                mHelper.enableDebugLogging(true, TAG);

                startPurchase();

            }
        }
    });

}
在我调用它的类中,我创建了该类的一个实例:

ThemeActivity themeact = new ThemeActivity();
我试图从我的类中的OnClick侦听器调用它:

new AlertDialog.Builder(getContext())
                        .setMessage(
                                "Would you like to purchase this theme?")
                        .setCancelable(false)
                        .setPositiveButton("Yes",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(
                                            DialogInterface dialog, int id) {
                                    //Here is where I am calling it 
                                     themeact.start(getContext());

                                    }
                                }).setNegativeButton("No", null).show();
我在LogCat中收到以下错误:

     FATAL EXCEPTION: main
 java.lang.NullPointerException
    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
    at test.test.util.IabHelper.<init>(IabHelper.java:164)
    at test.test.ThemeActivity.start(ThemeActivity.java:161)
    at test.test.ColorCard$1$1.onClick(ColorCard.java:67)
    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:171)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:213)
    at android.app.ActivityThread.main(ActivityThread.java:5225)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
    at dalvik.system.NativeStart.main(Native Method)
编辑


我意识到我一开始调用的方法是错误的,现在我调用的是正确的方法,它仍然会崩溃,出现一个空指针异常,并出现另一个错误。我试图传递上下文,但它仍然不起作用。每当我尝试调用方法start时;从创建我的活动开始,它就起作用了。但是,当试图从我的另一个类调用完全相同的方法时,它崩溃了,我不知道为什么。我从方法本身内部初始化所有内容,因此我不确定它为什么会以null崩溃。

尝试将活动的上下文变量作为参数传递给startPurchase方法。

最后,我通过将类移动到我的活动类中解决了我的问题。我不知道为什么它现在可以工作,但当我使用这个类作为一个单独的类时,它就不能工作了。

您在哪里定义它mHelper@EdGeorge在我的活动开始时。您是否从另一个活动(定义了该活动的活动除外)调用开始购买?@HeisenBerg否,我正在从另一个类调用开始购买的方法。@Jack mHelper为空-您需要分配它