in onStart() i want to set new values

1.7k views Asked by At
public class MainActivity extends ActionBarActivity {



    Button b1;

    TextView tv2;

    Integer count ;



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

        count++;


        b1= (Button)findViewById(R.id.b1);
        tv2=(TextView)findViewById(R.id.tv2);

        tv2.setText(count);

        if(count == 5) {
            Intent ii = new Intent(this,Activity2.class );

            Bundle bb = new Bundle();

            bb.putInt("Count",count);

            ii.putExtras(bb);

            startActivity(ii);

            finish();

        }

        else

        {
            Intent iii = new Intent(this,activity3.class);

             // Bundle bb1 = new Bundle();

            startActivity(iii);
        }
    }

    public void onStart()
    {


        Bundle b33 = getIntent().getExtras();

        count=b33.getInt("count");

        tv2.setText(count);
    }

in this code i want to count the number of times an activity is opened in this case activity1 will open only 5 times and after that activity3 will open what i m trying is killing the activity1 before killing i m sending the count value to the activity2 and after that i m calling again activity1 and again i m sending the count value to activity1 so the code will be executed from the start itself so again the value will be 1 but i want activity1 to catch the new values of count in oncreate() or in onstart()

and the error is the app is not opening its force closing and in logcat Null-exception error is displayed although i did all the bindings.

1

There are 1 answers

9
Anand Singh On

try this code at onStart() method

public void onStart() 
{ 
Bundle b33 = getIntent().getExtras();
if(b33 != null)
{

    count=b33.getInt("count");

    tv2.setText(count);
}
} 

change:

Integer count;

to

int count=0;

also change:

tv2.setText(count);

to(in both place onCreate and onStart):

tv2.setText(String.valueOf(count));

Updated code:

public class MainActivity extends ActionBarActivity {



Button b1;

TextView tv2;

int count = 0 ;



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

    count++;


    b1= (Button)findViewById(R.id.b1);
    tv2=(TextView)findViewById(R.id.tv2);

    tv2.setText(String.ValueOf(count));

    if(count == 5) {
        Intent ii = new Intent(this,Activity2.class );

        Bundle bb = new Bundle();

        bb.putInt("Count",count);

        ii.putExtras(bb);

        startActivity(ii);

        finish();

    }

    else

    {
        Intent iii = new Intent(this,activity3.class);

         // Bundle bb1 = new Bundle();

        startActivity(iii);
    }
}

 public void onStart()
{
    super.onStart();
    Bundle b33 = getIntent().getExtras();
    if(b33 != null)
    {

        count=b33.getInt("count");

        tv2.setText(String.valueOf(count));
    }
}

i tried this code and its working.