NullPointerException On Android.os.Bundle

前端 未结 3 2006
星月不相逢
星月不相逢 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:43

    if it in fragment you can use :

    if (getArguments() != null) {
    
              loginBody = getArguments().getParcelable(getActivity().getResources().getString(R.string.logInBodyParcelData));
        }
       /* } catch (Exception e) {
            e.printStackTrace();
        }*/
        if (loginBody != null) {
            setProfile();
    
        }
    
    0 讨论(0)
  • 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);
           }
        }
    
    0 讨论(0)
  • 2020-12-18 11:53

    Try this in your ViewCashFlow class:

    Intent i = new Intent(Context.getApplicationContext(), NewTransaction.class);
    
    0 讨论(0)
提交回复
热议问题