jess-anders / two-way-gridview

An Android GridView that can be configured to scroll horizontally or vertically
650 stars 261 forks source link

Got NullPointerException in TextView inside Two-way-gridView #38

Open ghost opened 8 years ago

ghost commented 8 years ago

Hi. I want to use TextView inside two-way-gridview. I can successfully use ArrayAdapter with two-way-gridview. It work correctly as I want. But when I adapt the example from http://stackoverflow.com/questions/22429829/how-to-add-a-textview-to-a-gridview-in-android. I got NullPointerException when accessing TextView. Here are my code.

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    static final String[] numbers = new String[] {
            "A", "B", "C", "D", "E",
            "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O",
            "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TwoWayGridView gridview = (TwoWayGridView) findViewById(R.id.gridview);
        gridview = (TwoWayGridView) findViewById(R.id.gridview);
//        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
//                android.R.layout.simple_list_item_1, numbers);
        TextViewAdapter adapter = (TextViewAdapter) new TextViewAdapter(this, numbers);
        gridview.setAdapter(adapter);
    }
}

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<com.jaikra.sarit.wecare.TwoWayGridView.TwoWayGridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridView1"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:columnWidth="100dp"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</com.jaikra.sarit.wecare.TwoWayGridView.TwoWayGridView>

item.xml

<TextView
    android:id="@+id/grid_item_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:layout_marginTop="5px"
    android:textSize="15px"
    xmlns:android="http://schemas.android.com/apk/res/android">
</TextView>

Problem: FATAL EXCEPTION: main java.lang.NullPointerException at com.jaikra.sarit.wecare.TextViewAdapter.getView(TextViewAdapter.java:54)

_Line 54 is : _

            TextView textView = (TextView) gridView
                    .findViewById(R.id.grid_item_label);

Question : Where am I wrong?