DaveNOTDavid
DaveNOTDavid

Reputation: 1803

App crashes when passing extras between activities

With these two java activities in Android Studio, I was wondering how to create objects/instances of another class (activity) AFTER converting the EditText variable type to an int upon getting input... Because whenever I run the code into my emulator and then press the onClick button to get to the SecondActivity screen, my app crashes:

UPDATED AS OF 1/31/2016, 5:55PM:

  1. MainActivity.java:

    package sample.sampleapp;

    import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.View; import android.widget.EditText;

    public class MainActivity extends AppCompatActivity implements View.OnClickListener { public EditText etHeight, etWidth; public int height, width;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
     }
    
     @Override
     public void onClick(View v) {
         Intent i = new Intent(this, SecondActivity.class);
    
         etHeight = (EditText)findViewById(R.id.enterHeight);
         String ht = etHeight.getText().toString();
         height = Integer.parseInt(ht);
         i.putExtra("height", height);
    
         etWidth = (EditText)findViewById(R.id.enterWidth);
         String wh = etWidth.getText().toString();
         width = Integer.parseInt(wh);
         i.putExtra("width", width);
    
         startActivity(i);
     }
    
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.menu_main, menu);
         return true;
     }
    

    }

  2. SecondActivity.java:

    package sample.sampleapp;

    import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.widget.TextView;

    public class SecondActivity extends AppCompatActivity { private TextView tvResult, tvResult2; public int height, width;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_second);
    
         Intent i = getIntent();
         height = i.getIntExtra("height", height);
         width = i.getIntExtra("width", width);
    
         tvResult = (TextView)findViewById(R.id.textViewResult);
         tvResult.setText(height);
         tvResult2 = (TextView)findViewById(R.id.textViewResult2);
         tvResult2.setText(width);
     }
    
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.menu_main, menu);
         return true;
     }
    

    }

Much appreciated!

Upvotes: 0

Views: 74

Answers (1)

ryanjohn
ryanjohn

Reputation: 86

Don't have those static variables in your first activity. Instead pass any parameters you need to between your activities by adding them into the intent as extras.

Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("height", height);
intent.putExtra("width", width);
startActivity(intent);

Then in SecondActivity retrieve those values from the intent in onCreate:

Intent intent = getIntent();
int height = intent.getIntExtra("height");
int width = intent.getIntExtra("width");

Upvotes: 0

Related Questions