2014-12-06 101 views
0

我有三個TextViews,plotText,yearText和directorText。還有一個用於用戶輸入的EditText。更新錯誤TextView

會發生什麼:當我按下使用提示按鈕時,應顯示年份。當我再次按下使用提示按鈕時,導演應該出現。

現在發生了什麼:當我按下使用提示按鈕時,年份顯示爲應該顯示。當我再次按下按鈕時,Director標籤顯示,但directorText(導演的名字)出現在EditText字段中,而不是我分配給它的TextView。

我已經檢查過我的標籤都是正確命名的,而且我正在分配給正確的標籤,並且我試過在Eclipse中清理項目,但那也不起作用。有任何想法嗎?

我定義TextViews中的onCreate:

TextView yearText = (TextView)findViewById(R.id.yearText); 
    TextView directorText = (TextView)findViewById(R.id.directorText); 
    TextView hintText = (TextView)findViewById(R.id.hintText); 
    TextView plotText = (TextView)findViewById(R.id.plotText); 

下面是按鈕的代碼:

public void useHint(View v){ 
    if (game.numHints == 2){  
     View y = findViewById(R.id.yearLabel); 
     y.setVisibility(View.VISIBLE); 
     yearText.setText(currentMovie.year); 
     game.numHints--; 
     hintText.setText("Hints Remaining: "+game.numHints); 
    }else if (game.numHints == 1){ 
     View d = findViewById(R.id.directorLabel); 
     d.setVisibility(View.VISIBLE); 
     directorText.setText(currentMovie.director); 
     game.numHints--; 
     hintText.setText("Hints Remaining: "+game.numHints); 
     // remove button 
     View b = findViewById(R.id.hintButton); 
     b.setVisibility(View.INVISIBLE); 
    } 
} 

這裏的XML:

<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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="Andrea.guessthetitle.ActivityGuessPage" > 

    <TextView 
     android:id="@+id/plotLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="Plot:" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/plotText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/plotLabel" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/plotLabel" 
     android:maxLines="10" 
     android:minHeight="500px" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/yearLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/plotText" 
     android:layout_below="@+id/plotText" 
     android:text="Year: " 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:visibility="invisible" /> 

    <TextView 
     android:id="@+id/yearText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/yearLabel" 
     android:layout_alignBottom="@+id/yearLabel" 
     android:layout_alignParentRight="true" 
     android:layout_toRightOf="@+id/yearLabel" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/directorLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/yearLabel" 
     android:layout_below="@+id/yearLabel" 
     android:text="Director: " 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:visibility="invisible" /> 

    <TextView 
     android:id="@+id/directorText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignBaseline="@+id/directorLabel" 
     android:layout_alignBottom="@+id/directorLabel" 
     android:layout_toRightOf="@+id/directorLabel" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/guessLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/directorLabel" 
     android:layout_below="@+id/directorLabel" 
     android:layout_marginTop="48dp" 
     android:text="Guess:" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <EditText 
     android:id="@+id/guessInput" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignBottom="@+id/guessLabel" 
     android:layout_toRightOf="@+id/guessLabel" 
     android:ems="10" > 

     <requestFocus /> 
    </EditText> 

    <Button 
     android:id="@+id/guessButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/endButton" 
     android:layout_alignLeft="@+id/guessLabel" 
     android:onClick="submitGuess" 
     android:text="Guess" /> 

    <Button 
     android:id="@+id/hintButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/guessButton" 
     android:layout_alignBottom="@+id/guessButton" 
     android:layout_alignParentRight="true" 
     android:onClick="useHint" 
     android:text="Use Hint" /> 

    <TextView 
     android:id="@+id/hintText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/hintButton" 
     android:text="Hints Remaining: 2" /> 

    <Button 
     android:id="@+id/endButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:onClick="endGame" 
     android:text="End Game" /> 

</RelativeLayout> 

下面是截圖(什麼也沒有鍵入到文本字段中,當我單擊現在消失的提示按鈕時,該文本出現在那裏因爲它應該這樣做): enter image description here

+1

嘗試清潔並建立您的項目。有時候ID不能正確更新。 – 2014-12-06 22:01:46

+1

胡人,我顯然跑了源 - >清理而不是項目 - >乾淨像我應該有。 :(現在在工作,謝謝。 – alundy 2014-12-06 22:10:12

回答

0

原來是一個愚蠢的問題,因爲這只是一個簡單的錯誤。儘管如此,我會留下來的,以防其他人遇到它。我不可能是唯一一個犯這樣小錯誤的人(希望!)。

問題是,我運行了源代碼 - >清理而不是項目 - >清理。運行正確的清潔做到了訣竅。感謝@Eugen Pechanec。