-1

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
            startActivity(ShowIntent);
        } else {
            Toast.makeText(getApplicationContext(), "Not working...", Toast.LENGTH_LONG).show();
        }

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

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.results);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(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);
        ValueS.setText(passSN);

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

            INR.setVisibility(View.VISIBLE);
            DR.setVisibility(View.VISIBLE);
            QTYR.setVisibility(View.VISIBLE);
            UR.setVisibility(View.VISIBLE);
            INR2.setVisibility(View.VISIBLE);
            DR2.setVisibility(View.VISIBLE);
            QTYR2.setVisibility(View.VISIBLE);
            UR2.setVisibility(View.VISIBLE);

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

            INR.setText(pIN1);
            DR.setText(pD1);
            pM1 = extras.getInt("Qty1", 0);
            passM1 = String.valueOf(pM1);
            QTYR.setText(passM1);
            UR.setText(pU1);
            INR2.setText(pIN2);
            DR2.setText(pD2);
            pM2 = extras.getInt("Qty2", 0);
            passM2 = String.valueOf(pM2);
            QTYR2.setText(passM2);
            UR2.setText(pU2);
        } 

        }

    }

Any ideas?

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

Mike
  • 75
  • 10
  • 1
    what your log says? – Vyacheslav Jul 19 '17 at 19:33
  • Don't know what the log says, Vyacheslav. Is there there a way to check the log, so I can report what I found? – Mike Jul 19 '17 at 19:37
  • use android studio to read the android log: stacktrace, etc. – Vyacheslav Jul 19 '17 at 19:38
  • https://developer.android.com/studio/debug/am-logcat.html – Vyacheslav Jul 19 '17 at 19:38
  • Thanks Vyacgeslav for the reply and link. I didn't use android studio log because for some reason when I run my apps on the virtual tablets. My virtual devices freeze and don't work. So, I just used my android tablet to test my apps now until I fix my freezing problem. – Mike Jul 19 '17 at 19:48
  • I imagine your activity allows for rotation correct? If this is the case and the crash happens after rotation it is because Android is breaking down your activity and when it rebuilds the activity it doesn't have a bundle (because you are not getting the same intent). If that is the case you will need to save all the data in onSavedInstanceState. If you can verify that the crash is on rotating the tablet I will give the full answer in the answers block. – Elliott Jul 19 '17 at 21:18
  • Also I should note that it doesn't make much sense to do a strings equals comparison when you have an int. You can simply just say ``` if(select == 2){ } ``` – Elliott Jul 19 '17 at 21:20
  • Hi Elliott, Thank you for the reply. I start up my tablet and didn't rotate and it crashed. I then close the app and reopened it. Then rotate the app and it crashed. – Mike Jul 19 '17 at 21:30
  • it would be much easier to help if you posted the error messages. Also you would do well to break the potential errors down into sections of code. Reduce the code down to something that will run and slowly add the additional code until you receive an error. Then you have pin pointed a problem to a line or two of code. Work on a solution to that then slowly add more until the next problem and so on. – Lew Perren Jul 20 '17 at 07:46
  • If you need to use your tablet for development then try AIDE, see https://play.google.com/store/apps/details?id=com.aide.ui&hl=en&referrer=utm_source%3Dgoogle%26utm_medium%3Dorganic%26utm_term%3Daide+java&pcampaignid=APPU_1_d21wWYn5FsTxUMD7p5gG There is free version and you will get the logcat error prints you need to check errors. I have no affliation to AIDE by the way. – Lew Perren Jul 20 '17 at 08:46
  • Hi BusinessPlanQuickBuilder, thank you for the replies. I check the AIDE and it not helping me with checking for errors with the app on my device. Also as for the error messages, I'm doing some research and by early next week, I will post a log with the error codes in the original post since I can't figure out why the error(s) are happen. – Mike Jul 21 '17 at 17:45

4 Answers4

0

try this on your New activity

if (passSN.equals("2")){
    //your code 
}
  • ValueS appears to be a textview and equating it to a string "2" should raise a error for sure. – Jayagopal Kannan Jul 19 '17 at 19:39
  • Thanks Jayagopai for pointing that out. I was busy trying to get the code setup and checking it to get the code to work that I don't think about that error. I will have to try ".getText().toString().equals("2 ")" and see if that works. Thank you again for pointing that out. – Mike Jul 19 '17 at 19:52
  • It's my Pleasure :) – Jayagopal Kannan Jul 19 '17 at 19:55
  • HI Jayagopai, I made the changes you suggested and I still getting a error message. I updated my code in the original post with the changes, you suggested to me to made. Did I type something wrong with the new code or is the problem maybe else where with my code. Should I post more of my code? Please let me know. Thanks – Mike Jul 19 '17 at 21:10
0

Looking at the documentation on the lifecycle of an activity suggests that your display components (textview etc) will not be available to set the visibility parameter until after the onCreate() has finished. This would suggest your if condition with visibility setting should be in the onStart() of the acivity.

Lew Perren
  • 1,209
  • 1
  • 10
  • 14
0

I am going to go ahead and post my assumption. It may be wrong which is totally fine. But based on the symptoms you are describing, it sounds like your activity is being killed and re-created because of a configuration change (rotating the device from portrait to landscape).

If this the fact then there is a possibility that Android is starting your activity up again without any of the bundled data you sent in the first time.

First you will need to do a null check on :

Bundle extras = getIntent().getExtras();

Secondly you will need to override the onSavedInstanceState and onRestoreInstance state to help re-populate your activity.

Lastly I believe you are unnecessarily converting an int to a string and then checking it's equality.

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

Can be turned into

if (select == 2) {
    //do something here
}
Elliott
  • 539
  • 4
  • 14
0

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!

Mike
  • 75
  • 10