The problem caused by this line:
ArrayAdapter adapter = new ArrayAdapter(this,R.layout.activity_main,titles);
The second parameter of ArrayAdapter
's constructor is a view (TextView
by default) which you populate with your data. When you're trying to populate R.layout.activity_main
that is a RelativeLayout
per se, you get ClassCastException
. If you're not going to use more complex layout than TextView
, use a preserved Android layout. Change the mentioned line to this one:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item,titles);