2017-11-23 7 views
0

Ich entwickle eine GridView-Aktivität in einem kleinen Projekt. Aber ich erhalte einen Fehler, wenn ich versuche, einen Verweis auf das XML-Element zu erstellen, das eine GridView ist. Ich bekomme einen Fehler, wenn ich versuche, findViewById Methode zu verwenden. Hier ist mein Java und XML-Code.findViewById-Methodenfehler ist aufgetreten

Java-Datei

package com.example.anu.gridviewcontry; 

    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.widget.GridLayout; 
    import android.widget.GridView; 

    public class MainActivity extends AppCompatActivity { 

     // Steps :- 
     /* 
      01) Prepare data source [images in drawble and names in string.xml] 
      02) Creat a GridView in xml (main layout file) 
      03) Bring GridView from xml to java (to fill values) 
      04) 
     */ 

     GridView myGridView; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      myGridView = (GridView) findViewByID (R.id.gridView); 
     } 
    } 

XML-Datei ist hier

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context="com.example.anu.gridviewcontry.MainActivity"> 

     <GridView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:id="@+id/gridView" 
      android:numColumns="auto_fit" 
      android:horizontalSpacing="10dp" 
      android:verticalSpacing="10dp" 
      android:columnWidth="120dp"> 
     </GridView> 

    </RelativeLayout> 
+1

oh ich vermisse einen Brief. danke –

+1

findViewByID (R.id.gridView); sollte sein: findViewById (R.id.gridView); – JamesSwinton

Antwort

3

Versuchen:

GridView myGridView = (GridView) findViewById(R.id.gridView); 
1

mit

Versuchen
myGridView = (GridView)findViewById(R.id.gridView); 

Dann Clean-Rebuild-Run.

Verwandte Themen