带有“getPackageName()”的空指针异常

时间:2015-06-14 01:59:54

标签: java android eclipse nullpointerexception

过去一周半,我一直坚持这个问题。我使用“Kilobolt”框架制作了一个游戏,我一直在尝试在我的应用程序中的App Purchase中实现,并且我已经尽可能地尝试了但是我一直得到相同的错误,即有一个空指针异常。

06-13 21:40:18.991:E / AndroidRuntime(16384):java.lang.NullPointerException 06-13 21:40:18.991:E / AndroidRuntime(16384):在android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)

我有一个活动,我有我的方法levelTwoButtonClicked(),我从java类调用该方法,这是空指针异常来自的地方,因为getPackageName()。< / p>

我在try catch语句中有这个:

skuDetails = mService.getSkuDetails(3, getPackageName(), "inapp", querySkus);

我做了一些研究,并且我读到上下文可能存在问题并且调用getPackageName()但是还没有能够找出问题,但感谢任何能帮我解决这个问题的人!

公共抽象类AndroidGame扩展Activity实现Game {

public static Context mContext;

IInAppBillingService mService;
ServiceConnection mServiceConn;

static final String ITEM_SKU = "android.test.purchased";

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
   getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

   boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    int frameBufferWidth = isPortrait ? 480: 800;
    int frameBufferHeight = isPortrait ? 800: 480;
    Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
            frameBufferHeight, Config.RGB_565);

    float scaleX = (float) frameBufferWidth
            / getWindowManager().getDefaultDisplay().getWidth();
    float scaleY = (float) frameBufferHeight
            / getWindowManager().getDefaultDisplay().getHeight();

    renderView = new AndroidFastRenderView(this, frameBuffer);
    graphics = new AndroidGraphics(getAssets(), frameBuffer);   
    fileIO = new AndroidFileIO(this);
    audio = new AndroidAudio(this);
    input = new AndroidInput(this, renderView, scaleX, scaleY);
    screen = getInitScreen();

    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);

    layout.addView(renderView);

    setContentView(layout);

    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyGame");

    mContext = getBaseContext();

    mServiceConn = new ServiceConnection() {
       @Override
       public void onServiceDisconnected(ComponentName name) {
           mService = null;
       }

       @Override
       public void onServiceConnected(ComponentName name,
          IBinder service) {
           mService = IInAppBillingService.Stub.asInterface(service);
       }
    };

    Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
    serviceIntent.setPackage("com.android.vending");
    bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);


}

public void levelTwoButtonClicked(){

    Assets.click.play(1.00f);
    ArrayList<String> skuList = new ArrayList<String> ();
    skuList.add("android.test.purchased");     
    Bundle querySkus = new Bundle();
    querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
    Bundle skuDetails;

                try {

                    skuDetails = mService.getSkuDetails(3, getPackageName(), "inapp", querySkus);

                    int response = skuDetails.getInt("RESPONSE_CODE");

                    if (response == 0) {

                        ArrayList<String> responseList = skuDetails.getStringArrayList("DETAILS_LIST");

                        for (String thisResponse : responseList) {
                            JSONObject object = new JSONObject(thisResponse);
                            String sku = object.getString("productId");
                            String price = object.getString("price");

                            if (sku.equals("android.test.purchased")) {

                                Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(), sku, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
                                PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
                                startIntentSenderForResult(pendingIntent.getIntentSender(), 1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));

                            }
                        }
                    }

                } catch (RemoteException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (SendIntentException e) {
                    e.printStackTrace();
                }

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == 1001) {
      int responseCode = data.getIntExtra("RESPONSE_CODE", 0);
      String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
      String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE");

      if (resultCode == RESULT_OK) {
         try {
            JSONObject jo = new JSONObject(purchaseData);
            String sku = jo.getString("android.test.purchased");
            Toast.makeText(mContext,"You have bought the " + sku + ". Excellent choice,adventurer!", Toast.LENGTH_LONG).show();
          }
          catch (JSONException e) {
             Toast.makeText(mContext,"Failed to parse purchase data.", Toast.LENGTH_LONG).show();
             e.printStackTrace();
          }
      }
   }
}



public static Context getContext(){
    return mContext;
}


@Override
public void onResume() {
    super.onResume();
    wakeLock.acquire();
    screen.resume();
    renderView.resume();
    mAdView.resume();

    if(Assets.mainmenuloop.isStopped()){
        Assets.mainmenuloop.play();
    }

}

@Override
public void onPause() {
    super.onPause();
    wakeLock.release();
    renderView.pause();
    screen.pause();
    mAdView.pause();

    if(Assets.mainmenuloop.isPlaying()){
        Assets.mainmenuloop.stop();
    }

    SharedPreferences HighScoreData = getSharedPreferences("high", 0);
    SharedPreferences.Editor editor = HighScoreData.edit();
    editor.putInt("score", highScore);
    editor.putInt("secondScore", secondHighScore);
    editor.putInt("thirdScore", thirdHighScore);
    editor.putInt("fourthScore", fourthHighScore);
    editor.commit(); 

    if (isFinishing())
        screen.dispose();
}

@Override
public Input getInput() {
    return input;
}

@Override
public FileIO getFileIO() {
    return fileIO;
}

@Override
public Graphics getGraphics() {
    return graphics;
}

@Override
public Audio getAudio() {
    return audio;
}

@Override
public void onDestroy() {
    super.onDestroy();
    mAdView.destroy();
    /*
    if (mService != null) {
        unbindService(mServiceConn);
    }
    */
}

@Override
public void setScreen(Screen screen) {

    if (screen == null)
        throw new IllegalArgumentException("Screen must not be null");

    this.screen.pause();
    this.screen.dispose();
    screen.resume();
    screen.update(0);
    this.screen = screen;

}


public Screen getCurrentScreen() {

    return screen;
}

}

//活动课程结束    // BEGIN REGULAR JAVA CLASS

public class InAppPurchase extends Screen {

AndroidGame Buy = new PurchaseLevels();

public InAppPurchase(Game game) {
    super(game);

}

@Override
public void update(float deltaTime) {
    // TODO Auto-generated method stub

    @SuppressWarnings("unused")
    Graphics g = game.getGraphics();
    List<TouchEvent> touchEvents = game.getInput().getTouchEvents();

    int len = touchEvents.size();
    for(int i = 0; i < len; i++) {
        TouchEvent event = touchEvents.get(i);
        if(event.type == TouchEvent.TOUCH_UP) {

            if(inBounds(event, 195, 142, 235, 55)) {
                // UNLOCK LEVEL TWO
                Buy.levelTwoButtonClicked();
            }   

            if(inBounds(event, 195, 310, 235, 55)) {
                // UNLOCK LEVEL THREE

            }

            if(inBounds(event, 195, 490, 235, 55)) {
                // UNLOCK LEVEL FOUR

            }

            if(inBounds(event, 70, 630, 210, 55)) {
                // RESTORE BUTTON
                Assets.click.play(1.00f);
            }

            if(inBounds(event, 330, 635, 130, 130)) {
                game.setScreen(new MainMenuScreen(game));
                Assets.click.play(1.00f);
            }

        }
    }

}


private boolean inBounds(TouchEvent event, int x, int y, int width,
        int height) {
    if (event.x > x && event.x < x + width - 1 && event.y > y
            && event.y < y + height - 1){
        return true;
    } else {
        return false;
    }
}

public void updateUI(){



}

@Override
public void paint(float deltaTime) {
    // TODO Auto-generated method stub

    }

}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public void resume() {
    // TODO Auto-generated method stub

}

@Override
public void dispose() {
    // TODO Auto-generated method stub

}

@Override
public void backButton() {
    // TODO Auto-generated method stub

}

}      //主要活动子类要让我在应用程序购买类中创建我的抽象主要活动“ANDROIDGAME”的实例,以便调用“LEVELTWOBUTTONCLICKED”

public class PurchaseLevels extends AndroidGame {

@Override
public Screen getInitScreen() {
    // TODO Auto-generated method stub
    return null;
}

}

0 个答案:

没有答案