I am creating a simple ListView app to learn more about programming for android.
However, this simple code (which is showing no errors whatsoever in Eclipse) is simply crashing on startup in the emulator. Any ideas?
public class MainActivity extends Activity {
static final String[] FRUITS = { "Apple", "Avocado", "Banana",
"Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
"Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" };
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_fruit);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_fruit, FRUITS);
ListView listView = (ListView) findViewById(R.layout.list_fruit);
listView.setTextFilterEnabled(true);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
list_fruit.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="20sp" >
</TextView>
Tried to find the stacktrace following your instructions, the only message that ame up was FATAL EXCEPTION: main.
For instance, this is wrong:
it should be something like:
where my_list_fruit is just the android:id tag assigned in the xml:
Try this, using android simple_list_view_1
}
}
OR define a list_item.xml with textview in it ex:
MainActivity.java
list_fruit.xml
list_item.xml
This:
is wrong for sure, because you need to look up
id
with findViewById, so for example like this, but you need to be sure about having a listview element with that id then:Change this
to
You have
and you use the same in the
ArrayAdapter
Also change
to
Also change to
Edit:
list_fruit.xml
does not have a ListView.You need the below in
list_fruit.xml
Then in
onCreate