1

我已将我的应用程序上传到 Google Play,经过几次更新后,我收到了这样的错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wipeout.simplegame/com.wipeout.simplegame.MainMenu}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.wipeout.simplegame.screen.MainMenu.onCreate(MainMenu.java:238)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more

这是我的 MainMenu.java

private ImageView advertButton;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_menu);
        advertButton = (ImageView) findViewById(R.id.advertOffButton);

    advertButton.setOnClickListener(new OnClickListener() { //line 238

        @Override
        public void onClick(View v) {

            if (mHelper != null) {
                mHelper.flagEndAsync();
                try {
                    mHelper.launchPurchaseFlow(MainMenu.this, SKU_PREMIUM, RC_REQUEST, mPurchaseFinishedListener, "");
                } catch (IllegalStateException ex) {
                }
            }
        }
    });
}

flagEndAsync()在 IabHelper 中设置为公众,因为我看到这对某人有帮助,但我的问题仍然存在。我从谷歌开发者网站复制的应用计费中的整个代码,所以我不知道问题出在哪里。对我和少数测试帐户来说,一切都很好,但有些人有问题。

4

2 回答 2

0

您的 AdvertButton 很可能null。在使用它之前,请确保您已通过activity.findViewById(R.id.advertButton);它。

于 2013-10-23T23:41:27.533 回答
0

试着让它像这样

findViewById(R.id.advertOffButton).setOnClickListener(MainMenu.this);

并添加覆盖方法 onClick

@Override
public void onClick(View view) {
   if (view.getId() == R.id.advertButton) {
      if (MainMenu.class != null) {
       if (mHelper != null) {
        mHelper.flagEndAsync();
        try {
               mHelper.launchPurchaseFlow(MainMenu.this, SKU_PREMIUM, RC_REQUEST, mPurchaseFinishedListener, "");
        } catch (IllegalStateException ex) {
        }
       }
    }
}
}

当然,您可以尝试使用 AND 运算符制作此 if 语句 :)

于 2013-10-24T11:26:15.633 回答