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 Implementing Designs for Android Customizing a GridView for Friends Converting a ListView to a GridView

Milan Tailor
Milan Tailor
5,132 Points

ListView to GridView Code Challenge (Self destructing messaging app) help

Hey,

What's the problem with my code here? I've changed it to extend activity rather than list activity. Declared the variable, however there seems to be a problem setting the GridView. I'm sure this is simple and I'm being thick.

This relates to part 2 of one of the code challenges.

activity_products.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>
ProductsActivity.java
import android.os.Bundle;
import android.widget.GridView;

// This parent class no longer applies. Switch it to Activity
public class ProductsActivity extends Activity {

    /*
     * Some code has been omitted for brevity!
     */

    // Declare mGridView here!
  protected GridView mGridView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_products);

        // Set mGridView here!
      mGridView = (GridView).findViewById(R.id.gridView);
    }
}

1 Answer

Maybe this was just a typo in posting to the forum, but you have a period in between the cast to (GridView) and findViewById(R.id.gridView), which would result in trying to call a static method on the GridView class rather than casting the result of the activity's findViewById(R.id.gridView) method. Should be

mGridView = (GridView) findViewById(R.id.gridView);
Milan Tailor
Milan Tailor
5,132 Points

What a numpty. Cheers!