2013-11-09 2 views
1

저는 sdk에 익숙해지기 위해 간단한 안드로이드 응용 프로그램을 작성하려고하는데 setText 메서드가 TextView를 변경하지 않는 이유를 알 수 없습니다. 기본적으로 내가 원하는 것은 사용자가 문자열을 입력하고, 사용자가 문자열에서 실행할 메소드를 선택하고 Textview로 출력을 설정하게하지만 TextView를 설정할 수없는 것입니다. onCreate에서 메서드를 호출하려고 시도했습니다. case에서 전환하고 각 경우에 하나도 출력 값으로 텍스트를 설정하지 않았습니다. 아무도 내가 뭘 잘못하고 있는지 알아?안드로이드 setText 메서드가 TextView에 텍스트를 설정하지 않았습니다.

package com.example.firstapp; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class TextChange extends Activity implements OnClickListener{ 

    Button findUpperCase, everyOther, vowelReplace, vowelCount; 
    EditText input; 
    TextView output; 
    String inputText, outputText; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.textchanger); 
     initializeVar(); 
     output.setText(outputText); 
    } 

    public void initializeVar(){ 
     findUpperCase = (Button) findViewById(R.id.finduppercase); 
     everyOther = (Button) findViewById(R.id.everyother); 
     vowelReplace = (Button) findViewById(R.id.vreplace); 
     vowelCount = (Button) findViewById(R.id.vcount); 
     input = (EditText) findViewById(R.id.stringinput); 
     output = (TextView) findViewById(R.id.output); 
    } 

    public static String findUpperCase(String x){ 
     //Initialize output value 
       String output = ""; 
     //For loop to loop through each character of the string 
       for (int i = 0; i <= x.length()-1; i++) 
       { 
     //Convert letter to char value and compare it to the ASCII values for capital letters 
        if (Character.isUpperCase(x.charAt(i))) 
        { 
     //Add each capital letter to the output string 
         output += x.substring(i, i+1) + " "; 

        } 
       } 
     //If not uppercase letters tell user 
       if (output.equals("")){ 
        output = "No upper case characters"; 
       } 
     //Return the capital letters or message to user and exit method 
       return output; 

      } 
     //Method used to get every other letter  
      public static String everyOther(String x){ 
     //Set initial value of output 
       String output = ""; 
     //Use for loop to get every other character by incrementing i by 2 each time 
       for(int i=0; i<=x.length() - 1; i+=2){ 
     //Set letter equal to a temperary holder 
        char temp = x.charAt(i); 
     //If it is the first letter add to output without a space preceeding it 
        if (i == 0){ 
     //New output is set as the old output plus the temp value without a space 
         output = output + temp; 
        } 
     //If it is not the first letter do this 
        else{ 
     //New output is set as the old output plus the temp value with a space 
        output = output + " " + temp; 
        } 
       } 
     //Return the output 
       return output; 

      } 
     //Method to replace the vowel with "_" 
      public static String vowelReplace(String x){ 
     //Search each string for vowels and ignore the case, when found replace with a "_" 
       String output = x.replaceAll("(?i)[aeiou]","_"); 
     //Return the output 
       return output; 
      } 
     //Method to count the vowels 
      public static int vowelCount(String x){ 
     //Set a counter  
       int count = 0; 
     //Set an array of char characters containing all the letters 
       char vowels[] = {'a','e','i','o','u','A','E','I','O','U'}; 
     //For loop to check each letter for vowels 
       for (int i = 0; i <= x.length() - 1; i++){ 
     //For loop to check each character against values in vowels array 
        for(int z = 0; z <= 9; z++){ 
     //If a vowel is found increment the count 
         if (x.charAt(i) == vowels[z]){ 
     //Increment the count 
          count++; 
         } 
        } 
       } 
     //Return the number of vowels 
       return count; 
      } 
     //Method to identify the vowel positions 
      public static String vowelPositions(String x){ 
     //Initialize an output variable 
        String output = ""; 
     //Set a temp string variable 
        String values = ""; 
     //Set an array of char characters containing all the letters 
        char vowels[] = {'a','e','i','o','u','A','E','I','O','U'}; 
     //For loop to check each letter for vowels 
         for(int z = 0; z < x.length(); z++){ 
     //For loop to check each character against values in vowels array     
          for (int i = 0; i <= 9; i++){ 
     //Check if each character is equal to any value in the array value 
           if (x.charAt(z) == vowels[i]){ 
     //If it is the first vowel found do not add a space before adding to output      
            if(output.equals("")){ 
     //Set first value of output 
             output = "[" + z + "]"; 
            } 
            else{ 
     //Add each position of the vowels to the output string 
             values = " [" + z + "]"; 
     //Set new value of outputs to old value plus the value of values 
             output = output + values; 
            } 
           } 

          } 
         } 
     //Return the location of the vowels       
         return "Vowels are located at the following indexes:" + output; 
        } 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       inputText = input.getText().toString(); 
       switch(v.getId()){ 
        case R.id.finduppercase: 
         outputText = findUpperCase(inputText); 
         break; 
        case R.id.everyother: 
         outputText = everyOther(inputText); 
         break; 
        case R.id.vcount: 
        outputText=Integer.toString(vowelCount(inputText)); 
         break; 
        case R.id.vpos: 
         outputText = vowelPositions(inputText); 
         break; 
        case R.id.vreplace: 
         outputText = vowelReplace(inputText); 
         break; 
       } 


      } 


} 
+0

TextView가 실제로 표시되었는지 확인 했습니까? TextView에서 getText를 호출하면 텍스트를 다시 인쇄 할 수 있습니까? – 2Dee

+0

onClick의 setText를 호출하십시오. – Manishika

+0

필자가 볼 수있는 한,'setText()'만'onCreate()'에 한 번만 호출합니다. – Simon

답변

1

당신은 아무데도 Onclick 방법 setText을 호출되지 않은 및

public void initializeVar(){ 
    findUpperCase = (Button) findViewById(R.id.finduppercase); 
      everyOther = (Button) findViewById(R.id.everyother); 
      vowelReplace = (Button) findViewById(R.id.vreplace); 
      vowelCount = (Button) findViewById(R.id.vcount); 
      findUpperCase.setOnClickListener(this); 
      everyOther.setOnClickListener(this); 
      vowelReplace.setOnClickListener(this); 
      vowelCount.setOnClickListener(this); 
    } 

2)가 온 클릭 방법에 여기의 setText

둘째로 당신은 당신의 버튼에 버튼

1) 설정 onClickListners에 리스너를 설정하지 않은 :

@Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       inputText = input.getText().toString(); 
       switch(v.getId()){ 
        case R.id.finduppercase: 
         outputText = findUpperCase(inputText); 
         output.setText(outputText); 
         break; 
        case R.id.everyother: 
         outputText = everyOther(inputText); 
         output.setText(outputText); 
         break; 
        case R.id.vcount: 
        outputText=Integer.toString(vowelCount(inputText)); 
        output.setText(outputText); 
         break; 
        case R.id.vpos: 
         outputText = vowelPositions(inputText); 
         output.setText(outputText); 
         break; 
        case R.id.vreplace: 
         outputText = vowelReplace(inputText); 
         output.setText(outputText); 
         break; 
       } 


     } 
1

는 아무도 내가 뭘 잘못 알고 있나요? onCreate()에서

, 당신은 TextViewsetText()를 호출하지만 output를 초기화하지 않은 당신이 전달하는 매개 변수는 null입니다.

코드에있는 다른 곳은 setText()입니다. 첫째

관련 문제