Home Why my app doesnt pass to the next activity
Reply: 3

Why my app doesnt pass to the next activity

tomer shavit
1#
tomer shavit Published in 2018-01-12 11:57:20Z

This question already has an answer here:

  • What is a NumberFormatException and how can I fix it? 9 answers

im trying to pass from the main activity to the second activity and also pass a number and it doesn't work can someone show my what my mistake is

p.s can anyone tell me how to do i make a loop that stops for the user to press the button to add text in the EditText and then reset the EditText and wait for him again to insert another text until an array is filled

thank you!!

public class MainActivity extends AppCompatActivity {

public static int numOfSails;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

try{
    Button nextButton = (Button) findViewById(R.id.next_button);
    EditText sailsNumET = (EditText) findViewById(R.id.sails_num);
    numOfSails = Integer.parseInt(sailsNumET.toString());
    nextButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            Intent addingIntent = new Intent(MainActivity.this,AddingActivity.class);
            addingIntent.putExtra("nos",numOfSails);
            startActivity(addingIntent);

        }
    });


     }catch(NumberFormatException e){

  }
 }
}

and in the second activity this is the code

public class AddingActivity extends AppCompatActivity {
public ArrayList<Sail> sails = new ArrayList<Sail>();
public static int numOfSails;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.adding_main);
    final Button addButton = (Button) findViewById(R.id.add_button);
    final EditText minusInGallonsET = (EditText) findViewById(R.id.gallons_num);
    final TextView counterET = (TextView) findViewById(R.id.counter_id);
    Bundle bundle = getIntent().getExtras();
    this.numOfSails = bundle.getInt("nos",0);
    for (int i = 0 ; i< numOfSails; i++){
        sails.add(new Sail());
    }

    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int counter = 0;
            for (Sail s : sails){
                s.setFuelInGallons(Integer.parseInt(minusInGallonsET.toString()));
                s.setSailNum(counter);
                counter++;
                counterET.setText(counter);
            }
               }
    });

 }
}
Saurabh Vadhva
2#
Saurabh Vadhva Reply to 2018-01-12 12:10:51Z

Please Check this code In Xml File

<EditText
    android:id="@+id/sails_num"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"/>

In Activity File

Button nextButton = (Button) findViewById(R.id.next_button);
        final EditText sailsNumET = (EditText) findViewById(R.id.sails_num);

        nextButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                final int numOfSails = Integer.parseInt(sailsNumET.getText().toString().equals("")?"0":sailsNumET.getText().toString());
                Intent addingIntent = new Intent(Main11Activity.this,AddingActivity.class);
                addingIntent.putExtra("nos",numOfSails);
                startActivity(addingIntent);

            }
        });
Jaanus
3#
Jaanus Reply to 2018-01-12 12:11:04Z

Something like that :

public class SecondActivity extends AppCompatActivity {

    public void backToMain (View view) {
        Intent intent = new Intent(getApplicationContext(),MainActivity.class);
       intent.putExtra("ID", your_string);  //Heres what you want to pass. "ID" is needed to catch that given Intent on second activity
       startActivity(intent);
    }

}

also set onClickListener to the button. :).

bnayagrawal
4#
bnayagrawal Reply to 2018-01-12 12:12:27Z

You are storing a string which is returned by object.toString() method. Which will not return user inserted value.

numOfSails = Integer.parseInt(sailsNumET.toString());

To get the user inserted value, use the below code.

numOfSails = Integer.parseInt(sailsNumET.getText().toString());

Note that editText.getText() returns a Editable object and not a string value, so you have to use toString() method.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308289 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO