0

i have a spinner with two values and a button to go to the next activity if selected the first the second activity will contain an image and text if the second is selected the second activity will contain another image and text but when i press the button to go to the second activity it always crashes

public class MainActivity extends AppCompatActivity {
    Spinner companies;
    Button infoButton;
    Button linkButton;
    ArrayList<String> alCompanies;
    ImageView cLogo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        companies = findViewById(R.id.main_sp_companies);
        infoButton = findViewById(R.id.main_btn_info);
        linkButton = findViewById(R.id.main_btn_link);
        alCompanies = new ArrayList<>();
        alCompanies.add("Google");
        alCompanies.add("Microsoft");
        ArrayAdapter<String> arrayAdapterCompanies = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_spinner_item, alCompanies);
        arrayAdapterCompanies.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        companies.setAdapter(arrayAdapterCompanies);
        infoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (companies.getSelectedItem().equals("Google")) {
                    Intent intent = new Intent(MainActivity.this, ActivityOne.class);
                    cLogo.setImageResource(R.drawable.googlelogo);
                    cLogo.setContentDescription(companies.getSelectedItem().toString());
                    startActivity(intent);
                }
                else {
                    companies.getSelectedItem();
                    Intent intent = new Intent (MainActivity.this,ActivityOne.class);
                    cLogo.setImageResource(R.drawable.micrsoftlogo);
                    cLogo.setContentDescription(companies.getSelectedItem().toString());
                    startActivity(intent);
                }
            }
        }); 

this the mian

public class ActivityOne extends AppCompatActivity {
    public static final String EXTRA_INFO = "info";

    //Spinner companies = findViewById(R.id.main_sp_companies);
    ImageView cLogo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
        Intent intent = getIntent();
      /* if (companies.getSelectedItem().equals("Google")) {
            cLogo.setImageResource(R.drawable.googlelogo);
           cLogo.setContentDescription(companies.getSelectedItem().toString());
        }
        else {
           companies.getSelectedItem();
            cLogo.setImageResource(R.drawable.micrsoftlogo);
           cLogo.setContentDescription(companies.getSelectedItem().toString());
        }*/
    }
}

this is the first activity

i want the user chose from the spinner and depends on their choice will be the second activity if Google was chosen the logo and the description will be the one about google but Microsoft will be about the Microsoft

  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Feb 02 '23 at 18:51
  • How to [Get spinner selected items text?](https://stackoverflow.com/questions/5787809/get-spinner-selected-items-text) – Enowneb Feb 03 '23 at 07:36

1 Answers1

0

I find it very helpful when I look at error logs and ask or google a question based on that. because Error logs give you a clue on to what happened and why.

in your question, my guess would be your if clause in if (companies.getSelectedItem().equals("Google")) { . selected item may not be a String. and to acces selected String value, try to add onItemSelectedistener to companies(Spinner)

a quick look on android developers page gives you an example like the following.

Spinner companies = findViewById(R.id....);
String selected;

companies.setOnItemSelectedListener(new AdapterView.onItemSelectedListener(){

    public void onItemSelected(AdapterView<?> parent, View view,
            int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        selected = parent.getItemAtPosition(pos); // Use this in your onClick.

    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }


});

TG. Kahsay
  • 114
  • 5