Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Lists and Adapters (2015) Standard ListViews Using a Default Adapter

setListAdapter(adapter) not working

My project is able to build and run on my device, except when I click on the 7 DAY button, the screen goes blank white when it should display the array of Strings daysOfTheWeek. My code is:

package com.teamtreehouse.stormy;

import android.app.ListActivity; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.ListView;

public class DailyForecastActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_daily_forecast);

    String[] daysOfTheWeek = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, daysOfTheWeek);
    setListAdapter(adapter);
}

}

2 Answers

David Axelrod
David Axelrod
36,073 Points

I had the same error. make sure the code is within the onCreate method. There should be two curly braces after setListAdapter

Does your R.layout.activity_daily_forecast contain a listview with the ID "@android:id/list"?

ListActivity by default has a layout. You using setContentView(...); overrides this and it will look inside this layout for something with the id @android:id/list

Yes, my XML file does have a ListView with the id @android:id/list. Here is my XML code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.teamtreehouse.stormy.DailyForecastActivity" android:background="@drawable/bg_gradient" android:id="@android:id/empty">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/list"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/no_daily_forecast_data"
    android:id="@android:id/empty"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:textColor="#ffffffff"/>

</RelativeLayout>

Your top level element (it's cut off, but I assume it's a relative layout) has the id "@android:id/empty" and so does the text view. I can only assume that's causing some issues.