2017-01-06 39 views
1

我在一個片段中工作,我希望用戶輸入一個答案,然後當點擊一個按鈕時,一個文本視圖應該顯示正確。 這是我的代碼Android studio EditText不加載TextView

public class FragmentOne extends Fragment { 

    private EditText userAnswer; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View v = inflater.inflate(R.layout.fragment_fragment_one, null); 
     userAnswer = (EditText)v.findViewById(R.id.editText); 
     final String theAnswer = (userAnswer.getText().toString()); 
     Button submit = (Button)v.findViewById(R.id.submitBtn1); 
     submit.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       if (theAnswer.equalsIgnoreCase("Germany")) { 
        TextView tv = (TextView)v.findViewById(R.id.showCorrect); 
        tv.setText("Correct!"); 
       } 
      } 
     }); 
     return v; 


    } 
} 

然而,當德國類型,TextView的仍然只是顯示 'X'(我將它設置爲最初顯示)

有關XML的是

<Button 
    android:text="Submit" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="30dp" 
    android:id="@+id/submitBtn1" 
    android:layout_below="@+id/editText" 
    android:layout_centerHorizontal="true" /> 

<EditText 
    android:layout_width="220dp" 
    android:layout_height="70dp" 
    android:inputType="textPersonName" 
    android:hint="Write answer and press enter" 
    android:ems="10" 
    android:id="@+id/editText" 
    android:textSize="16sp" 
    android:layout_below="@+id/imageView" 
    android:layout_centerHorizontal="true" /> 

<TextView 
    android:text="x" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/showCorrect" 
    android:layout_below="@+id/skipBtn" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="44dp" /> 

出了什麼問題?

+0

您試圖在按鈕上查找textView TextView tv =(TextView)v.findViewById(R.id.showCorrect);' 意味着當您單擊對象時onClick(View v)'獲取您單擊視圖中的對象。所以你需要用你的userAnswer對象初始化 – user5599807

+0

@ user5599807你是什麼意思用userAnswer對象初始化?你可以更清楚地瞭解我應該更改的代碼 –

回答

1

你必須改變這一行:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View v = inflater.inflate(R.layout.fragment_fragment_one, null); 
    userAnswer = (EditText)v.findViewById(R.id.editText); 
    TextView tv = (TextView)v.findViewById(R.id.showCorrect); 
    Button submit = (Button)v.findViewById(R.id.submitBtn1); 
    submit.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      String theAnswer = (userAnswer.getText().toString()); 
      if (theAnswer.equalsIgnoreCase("Germany")) { 
       tv.setText("Correct!"); 
      } 
     } 
    }); 
    return v; 


} 

即:在你的代碼userAnswer值始終是相同的(未選中的按鈕點擊)

+0

我將代碼更改爲此,但是當我按下提交時,崩潰了應用程序 –

+0

@KaraW發佈您的錯誤請 –

+0

沒有錯誤,無法獲取堆棧跟蹤顯示任何內容。它在更改代碼之前沒有崩潰 –

1

代碼需要改爲:

public class FragmentOne extends Fragment { 

    private EditText userAnswer; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View v = inflater.inflate(R.layout.fragment_fragment_one, null); 
     userAnswer = (EditText)v.findViewById(R.id.editText); 
     final TextView tv = (TextView)v.findViewById(R.id.showCorrect); //changed 
     Button submit = (Button)v.findViewById(R.id.submitBtn1); 
     submit.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       String theAnswer = (userAnswer.getText().toString()); 
       if (theAnswer.equalsIgnoreCase("Germany")) { 
        //TextView tv = (TextView)v.findViewById(R.id.showCorrect); 
        tv.setText("Correct!"); 
       } 

       // updateScore(); 
      } 
     }); 
     return v; 
    }