1

I am trying to learn Android Studio and my first app is a blood alcohol calculator. The user starts that app and then a new activity is started so that they can enter their weight and press ok, this returns them back to the main activity and fills in the weight text.

I use startActivityForResult and then putExtra in the second activity. The first activity crashes if I use the getExtra method, if I delete the 2 receiving lines of code then there is no crash. When I use the debugger it says NullPointerException just before it says App has stopped working

Main activity code

    public class MainActivity extends Activity {
      static int displayunit = 0;
      static double percentage = 5.0;
      static int change = 1;
      static double bah;
      static double vol = 25;
      static double timestamp = 0;
      static double w = 0;
      static String we = "a";
      final int rcode = 3;
      final String[] units = {"Small Shot(25ml)", "Large Shot(35ml)", "Small 
    Port/Sherry(50ml)", "Large Port/Sherry(70ml)", "Small Wine(125ml)", 
    "Large 
    Wine(175ml)", "Small Beer Bottle(284ml)", "Half Pint(236.6ml)", "Medium 
    Beer Bottle(330ml)", "Can of beer(440ml)", "Large Bottle(500ml)", 
    "Pint(568.26ml)", "Massive Beer Bottle(660ml)"};
    final int[] unitsum = {25, 35, 50, 70, 125, 175, 284, 237, 330, 440, 500, 
    569, 660};

      @Override

      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent();





 intent.setClassName("com.example.alccalc","com.example.alccalc.enterweight");
            if(w ==0){
             startActivityForResult(intent, rcode);
            }

      }

            @Override
            protected void onActivityResult ( int requstCode, int resultCode, 
    Intent intent){
                if (requstCode == rcode && resultCode == RESULT_OK) {
                    we = getIntent().getStringExtra("weighttext");
                    w = Double.parseDouble(we);
                }
                TextView kg = (TextView) findViewById(R.id.kg);
                kg.setText(we)

Second Activity

    public class enterweight extends Activity {

    EditText entweight;
    TextView tester;
    String weightstring;

    @Override
    protected void onCreate(Bundle State) {
    super.onCreate(State);
    setContentView(R.layout.activity_enterweight);

    entweight = (EditText) findViewById(R.id.entweight);
    tester = (TextView)findViewById(R.id.tester);


    Button okweight = (Button) findViewById(R.id.okweight);
    okweight.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            weightstring = entweight.getText().toString();
            //tester.setText(weightstring);

                Intent intent = new Intent();
                intent.putExtra
                        ("weighttext", weightstring);
                setResult(RESULT_OK, intent);
                if (intent.hasExtra("weighttext")) {
                    finish();
                }


        }
    });
    }
    }
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
percy
  • 27
  • 5
  • Use Logcat to examine the stack trace associated with the crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this If you do not understand the stack trace, edit your question and include the stack trace. – CommonsWare Aug 19 '19 at 16:14
  • You are on the right track by trying to use a debugger. Can you tell us on exactly which line the activity is crashing? – Tim Biegeleisen Aug 19 '19 at 16:15

3 Answers3

0

Try this in your receiving code

Intent intent= getIntent();
Bundle b =  intent.getExtras();
if(b != null)
we = b.getString("weighttext");
Pradeep
  • 261
  • 2
  • 7
0

The error is here getIntent();

Since you are using the onActivityResult ( int requstCode, int resultCode, Intent intent) method, you can find your extras in the variable intent. The method getIntent() returns the intent that is used to start the activity. In your case it is null.

Use:

we = intent.getStringExtra("weighttext");

instead of

we = getIntent().getStringExtra("weighttext");

Better:

        Bundle extras = intent.getExtras();
        if (extras != null) {
           String result = extras.getString("weighttext");
           .....
        }
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • Thanks, that makes sense and I tried it and it worked. Amazing to see it actually work and not crash after weeks of crashing. – percy Aug 21 '19 at 15:24
  • Hi @percy if this or any answer has solved your question please consider [accepting](https://meta.stackexchange.com/q/5234/179419) it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Gabriele Mariotti Aug 21 '19 at 15:27
0

You are calling getIntent() to get the result data. getIntent() is an activity method that returns an intent that is used to start the activity. You should use the intent variable that is passed on onActivityResult method.

if (requstCode == rcode && resultCode == RESULT_OK) { we = intent.getStringExtra("weighttext"); w = Double.parseDouble(we); }

Shaiful
  • 5,643
  • 5
  • 38
  • 41