在try / catch里面的Android java.lang.NullPointerException

时间:2014-08-26 17:09:31

标签: java android exception

我有以下代码(app live,我在mService.consumePurchase的行中报告了崩溃):

Thread trd = new Thread(new Runnable()
{
      @Override
      public void run()
      {
          try
          {
                int response = mService.consumePurchase(3, getPackageName(), sku.mToken);
                if (response == 0)
                {
                    System.out.println("onActivityResult consume ok");
                    purchasesOK.add(sku.mSku);
                }
                else
                {
                    System.out.println("onActivityResult consume failed");
                    purchasesFail.add(sku.mSku);
                    runOnUiThread(new Runnable()
                    {
                         public void run() 
                         {
                            alert("Store Problems!", "There were problems consuming the product, please use RESTORE button.");
                         }
                    });
                }
          } catch (RemoteException e) {
              purchasesFail.add(sku.mSku);
                System.out.println("onActivityResult consume crashed");
                runOnUiThread(new Runnable()
                {
                     public void run() 
                     {
                     alert("Store Problems!", "There were problems consuming the product, please use RESTORE button.");
                     }
                });
            }
     }
});
trd.start();

这是崩溃报告:

  

java.lang.NullPointerException at   com.XXXXX.GGActivity.j_ItemPurchase(GGActivity.java:1309)at at   com.XXXXX.GGLib.step(Native Method)at   com.XXXXX.GGView $ Renderer.onDrawFrame(GGView.java:436)at at   android.opengl.GLSurfaceView $ GLThread.guardedRun(GLSurfaceView.java:1523)   在android.opengl.GLSurfaceView $ GLThread.run(GLSurfaceView.java:1240)

我认为try / catch会在任何异常情况下显示弹出错误。什么可能导致它的想法?或者我该如何避免呢?

谢谢, / C

1 个答案:

答案 0 :(得分:1)

添加:

catch(NullPointerException e){
    purchasesFail.add(sku.mSku);
    System.out.println("onActivityResult consume crashed");
    runOnUiThread(new Runnable(){
        public void run(){
            alert("Store Problems!", "There were problems consuming the product, please use RESTORE button.");
        }
    });
}

以显示弹出消息