Mike Mike - 1 year ago 60
Java Question

How to do an if statement in an onCreate method of a new activity?

I was wondering if anyone could help me with a problem I'm having with if statements in the onCreate method. I'm making a button that passes information on to another activity and then that activity will display the information based on if statements I setup in the onCreate method code. Now I was looking around for information on "How to do if statements in onCreate methods?" and found this question about it. So, I change my code to make it similar to the code used in the answer but I keep getting an "App stopping working" error message on my tablet. So, I was wondering if anyone could help me and tell me if I coded something wrong or if I need to pass the information to the new activity a different way? or if I need to add anything to the code that I missing?

Thank you in advance!

My code

Main Activity: (code in onclick method)

if (select == 2) {
Intent ShowIntent = new Intent(MainActivity.this, results.class);
ShowIntent.putExtra("SN", select); // int
ShowIntent.putExtra("IN1", IN1T); // string
ShowIntent.putExtra("D1", D1T); // string
ShowIntent.putExtra("Qty1", M1); // int
ShowIntent.putExtra("U1", U1T); // string
ShowIntent.putExtra("IN2", IN2T); // string
ShowIntent.putExtra("D2", D2T); //string
ShowIntent.putExtra("Qty2", M2); // int
ShowIntent.putExtra("U2", U2T); // string
} else {
Toast.makeText(getApplicationContext(), "Not working...", Toast.LENGTH_LONG).show();

new activity (updated code - 7/19 5:06)

protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
ValueS = (TextView) findViewById(R.id.txtselectv);
welcome = (TextView) findViewById(R.id.txtWelcome);
start =(TextView) findViewById(R.id.txtStarted);
INR = (TextView) findViewById(R.id.txtIN1R);
DR = (TextView) findViewById(R.id.txtD1R);
QTYR = (TextView) findViewById(R.id.txtQty1R);
UR = (TextView) findViewById(R.id.txtU1R);
INR2 = (TextView) findViewById(R.id.txtIN2R);
DR2 = (TextView) findViewById(R.id.txtD2R);
QTYR2 = (TextView) findViewById(R.id.txtQty2R);
UR2 = (TextView) findViewById(R.id.txtU2R);

Bundle extras = getIntent().getExtras();
select = extras.getInt("SN", 0);
passSN = String.valueOf(select);

if (passSN.equals("2")){


pIN1 = extras.getString("IN1");
pD1 = extras.getString("D1");
pU1 = extras.getString("U1");
pIN2 = extras.getString("IN2");
pD2 = extras.getString("D2");
pU2 = extras.getString("U2");

pM1 = extras.getInt("Qty1", 0);
passM1 = String.valueOf(pM1);
pM2 = extras.getInt("Qty2", 0);
passM2 = String.valueOf(pM2);



Any ideas?

Update: My problem is solved (Explain in answer post)

Answer Source

Problem Solved

I tested my app in android studio on another computer. I will able to find a virtual device to work and I found an java error. I can't remember exactly but it had to do something with my called Toolbar in the Oncreate method and me setting @style/AppTheme instead of @style/NoActionBar and because I had @style/AppTheme. It cause an error with my new activity and forced the app to crash. So, I made the changes and it works.

Thank everyone very much for the code suggestions. As I was check the different code suggestions out, I found several other errors out and wanted to thank everyone for your suggestions. I would have never seen or caught these errors if everyone would have not suggestion check out my code and making changes here or there. Also if you instead, the article I found on this error is here.

Thank you everyone again for your help!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download