SCB Gaming Presents
SCB Gaming Presents

Reputation: 39

Android changing activity issues, crashes on selection

So, what I'm trying to do is have a main menu and then press a button, it loads a list of countries.

now I've seemed to have set it up right, no errors I can see, but the app crashes before I load this activity (the lists) the main menu is fine, and I added another button with a blank activity which loads just fine.

logCat is giving me these errors

E/ArrayAdapter: You must supply a resource ID for a TextView
D/AndroidRuntime: Shutting down VM    
                  java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

and I'm not sure what it's asking for beyond the textView

heres my XML:

<ListView
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="#f897"
    android:dividerHeight="1dp"
    android:listSelector="#0f0"/>

and my Java:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class countries extends AppCompatActivity {

        ListView simpleList;
        String countryList[] = {"India", "China", "Australia", "Portugal", "USA","England", "NewZealand", "Germany", "France","South Africa"};


        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_countries);
            simpleList = (ListView)findViewById(R.id.text1);
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_countries,  countryList);
            simpleList.setAdapter(arrayAdapter);
        }


}

any help?

Upvotes: 0

Views: 52

Answers (3)

SonVi
SonVi

Reputation: 91

change:

 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_countries,  countryList);

to:

 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1,countryList);

your code not work because you use default ArrayAdapter with custom item layout

Upvotes: 1

Liar
Liar

Reputation: 1255

You should create a custom adapter and custom view for each listview's row. Check it here: Custom Adapter for List View

Upvotes: 1

dazza5000
dazza5000

Reputation: 7608

You array adapter expects the layout to be a TextView, but it look like R.layout.activity_countries is an activity layout?

Upvotes: 1

Related Questions