为什么会出现错误-类型不兼容?

时间:2020-03-28 21:00:12

标签: java android types incompatibletypeerror

public class MainActivity extends AppCompatActivity{

 Spinner spinner1;
 TextView wynikTV;
 EditText ileET;
 String[] waluty = new String[]{"EUR", "USD", "GBP", "SEK"};
 int wybranaWaluta=0;
 private Object Menu;


@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.settings_item) {
        Intent intent = new Intent(this, KursyActivity.class);
        intent.putExtra("waluty",(ArrayList<Waluta>)waluty);
        startActivityForResult(intent,1);
    }
    return super.onOptionsItemSelected(item);
}

错误:不兼容的类型:String []无法转换为ArrayList intent.putExtra(“ waluty”,(ArrayList)waluty);

我为什么要得到这个?

1 个答案:

答案 0 :(得分:0)

问题在以下行中:

intent.putExtra("waluty",(ArrayList<Waluta>)waluty);

在这里,您尝试将String[]强制转换为ArrayList,因为ArrayList不会扩展String[]

按照

intent.putExtra("waluty",new ArrayList<String>(Arrays.asList(waluty)));