2015-05-23 2 views
0

Я не могу получить findViewById(), работая с EditText. Он отлично работает с кнопкой, но я думаю, что я делаю что-то неправильно, и я не могу найти решение нигде. Как работает findViewById() с EditText?findViewByID в Android Studio не работает с EditText

Вот мой класс:

public class MainActivity extends ActionBarActivity { 

    Button sbutton; 
    EditText book, chapter, verse; 

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

     sbutton = (Button) findViewById(R.id.sbutton); 

     book = (EditText) findViewByID(R.id.book1); 
     chapter = (EditText) findViewByID(R.id.chapter1); 
     verse = (EditText) findViewByID(R.id.verse1); 
    } 
} 

А вот мой XML:

<TextView android:text="Please enter your favorite scripture" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/book1" 
    android:layout_below="@+id/textView" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="60dp" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/chapter1" 
    android:layout_below="@+id/textView" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="100dp" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/verse1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="150dp" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Submit" 
    android:id="@+id/sbutton" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="77dp" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Book" 
    android:id="@+id/textView2" 
    android:layout_alignTop="@+id/book" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Chapter" 
    android:id="@+id/textView3" 
    android:layout_below="@+id/book" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Verse" 
    android:id="@+id/textView4" 
    android:layout_below="@+id/chapter" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

Я знаю, что это может быть не самый хороший код, потому что я совершенно новый для Android Studio. Я был бы признателен за небольшую помощь в понимании того, как этот метод работает и что я могу делать неправильно здесь. Благодаря!

ответ

4

Вы делаете это как "findViewByID()", но его findViewById()

sbutton = (Button) findViewById(R.id.sbutton); 
book = (EditText) findViewByID(R.id.book1); 
chapter = (EditText) findViewByID(R.id.chapter1); 
verse = (EditText) findViewByID(R.id.verse1); 

Правильное имя метода

findViewById() 

sbutton = (Button) findViewById(R.id.sbutton); 

book = (EditText) findViewById(R.id.book1); 
chapter = (EditText) findViewById(R.id.chapter1); 
verse = (EditText) findViewById(R.id.verse1);