NullPointerException On Android.os.Bundle

前端 未结 3 2018
星月不相逢
星月不相逢 2020-12-18 11:23

I have some problem with my code, when I need to transfer some data from one Activity to another one. First Activity (ViewCashflow) a

3条回答
  •  太阳男子
    2020-12-18 11:50

    This is happen because when you start your second activity NewTransaction directly you don't put extras in the intent, so when you call getIntent().getExtras(); it returns a null object and this is why getIntent().getExtras().getBoolean("update"); throw the NPE.

    As solution: try to check if getIntent().getExtras() != null before getting the data, this will fix your problem.

    Bundle bundle= getIntent().getExtras();
        if (bundle!= null) {// to avoid the NullPointerException
            isUpdate=bundle.getBoolean("update");
            if(isUpdate)
            {
               id=bundle.getString("TransId");
               transname=bundle.getString("TransName");
               transamount=bundle.getString("TransAmount");
               transtype=bundle.getString("TransType");
               transdate=bundle.getString("CategDate");
               transcategid=bundle.getString("CategCategId");
               txtCashflow.setText(transname);
               txtType.setText(transtype);
               txtAmount.setText(transamount);
           }
        }
    

提交回复
热议问题