Android Studio 根据微调选择在Activity中启动不同的内容

6kkfgxo0  于 2023-02-05  发布在  Android
关注(0)|答案(1)|浏览(156)

我有一个带有两个值的微调器和一个按钮,如果选择了第一个,则转到下一个活动第二个活动将包含图像和文本如果选择了第二个,则第二个活动将包含另一个图像和文本,但当我按下按钮转到第二个活动时,它总是崩溃

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);
                }
            }
        });

这是我的

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());
        }*/
    }
}

这是第一个活动
我希望用户从旋转器中选择,并取决于他们的选择将是第二个活动,如果谷歌被选中的标志和描述将是一个关于谷歌,但微软将关于微软

cx6n0qe3

cx6n0qe31#

我发现当我查看错误日志并在此基础上问或谷歌一个问题时非常有帮助。因为错误日志给予你一个关于发生了什么和为什么发生的线索。
在您问题中,我猜是if (companies.getSelectedItem().equals("Google")) {中的if子句。选定项可能不是字符串。要访问选定的字符串值,请尝试将onItemSelectedistener添加到companies(Spinner)
快速浏览一下android开发者页面,你会看到一个类似下面的例子。

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
    }

});

相关问题