0

I am working on android activity life-cycle. But i have a problem with connections.

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

    speedValue = (TextView) findViewById(R.id.speed_value);
    rpmValue = (TextView) findViewById(R.id.rpm_value);
}

@Override
public void onResume(){
    Intent intent = getIntent();
    Bundle input = intent.getExtras();

    displayOperations(input);
    super.onResume();
}

public void displayOperations(Bundle input){

    boolean flag = input.getBoolean("flag");
    if(flag){
        for (int i = 0; i < input.getStringArrayList("second").size(); i++) {
            rpmValue.setText(input.getStringArrayList("rpm").get(i));
            speedValue.setText(input.getStringArrayList("speed").get(i));
        }
    }
}

//Menünün içeriği burada ayarlanır.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent;
    switch (item.getItemId()) {
        case R.id.action_settings:
            intent = new Intent(this, Settings.class);
            startActivity(intent);
            return true;

        case R.id.load_button:
            intent = new Intent(this, Operations.class);
            startActivity(intent);
            onStop();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

In this code im tying to get information from other activity. have to go that class and do some operations then come to our main class to be success. Our bundle comes from other activity as you see. There problem occurs right here. My bundle comes null because i couldnt go to second activity.

When i try onResume, application crushes because of null pointer. How can i solve this issue

LOGCAT

 Attempt to invoke virtual method 'boolean android.os.Bundle.getBoolean(java.lang.String)'
     on a null object reference
         at com.akaydin.berkin.carmonitor.MainActivity.displayOperations(MainActivity.java:42)
        at com.akaydin.berkin.carmonitor.MainActivity.onResume(MainActivity.java:36)
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
Berkin
  • 1,565
  • 5
  • 22
  • 48

1 Answers1

0

This is normal.

getIntent() only returns the starting intent, not the one when resuming

So you have several options:

  1. You use a static class : Where you temporarly store the data and access it then from the other class

  2. You call onNewIntent() : You use onNewIntent, which is called before onResume, to ovveride the older intent and replace it by a new one @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); // getIntent() should always return the most recent setIntent(intent); } Then you keep the code you have.

As you already did some code, best option for you is the 2. So just add the code somewhere in the main class :)

Hope this helped :)

Cukic0d
  • 5,111
  • 2
  • 19
  • 48