Java Android setText方法不支持';t在文本视图中设置文本

Java Android setText方法不支持';t在文本视图中设置文本,java,android,Java,Android,我正试图编写一个简单的android应用程序来熟悉sdk,但我不明白为什么setText方法不改变TextView。基本上,我希望用户输入一个字符串,让用户选择在字符串上运行哪个方法,并将输出设置为Textview,但我似乎无法设置Textview。我曾尝试在onCreate、cases之后的开关中以及在每种情况下调用该方法,但没有人将文本设置为输出值。有人知道我做错了什么吗 package com.example.firstapp; import android.app.Activity;

我正试图编写一个简单的android应用程序来熟悉sdk,但我不明白为什么setText方法不改变TextView。基本上,我希望用户输入一个字符串,让用户选择在字符串上运行哪个方法,并将输出设置为Textview,但我似乎无法设置Textview。我曾尝试在onCreate、cases之后的开关中以及在每种情况下调用该方法,但没有人将文本设置为输出值。有人知道我做错了什么吗

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;
                }


            }


}
package com.example.firstapp;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
公共类TextChange扩展活动实现OnClickListener{
按钮FindPerCase、everyOther、元音替换、元音计数;
编辑文本输入;
文本视图输出;
字符串inputText,outputText;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.textchanger);
initializeVar();
output.setText(outputText);
}
public void initializeVar(){
findUpperCase=(按钮)findviewbyd(R.id.findUpperCase);
everyOther=(按钮)findViewById(R.id.everyOther);
元音替换=(按钮)findViewById(R.id.vreplace);
元音计数=(按钮)findViewById(R.id.vcount);
输入=(EditText)findViewById(R.id.stringinput);
输出=(TextView)findViewById(R.id.output);
}
公共静态字符串findUpperCase(字符串x){
//初始化输出值
字符串输出=”;
//For循环遍历字符串的每个字符
对于(int i=0;i
有人知道我做错了什么吗

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;
                }


            }


}
onCreate()
中,您正在
TextView
上调用
setText()
,但您传递的参数是
null
,因为您尚未初始化
输出


代码中没有其他地方可以调用
setText()

首先,在
Onclick
方法中没有调用
setText
,其次,没有将侦听器设置为按钮

1) 将onClickListners设置为您的按钮

 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) 在onClick方法中的setText:

 @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;
                }


        }

您是否检查了TextView是否实际可见?如果您在TextView上调用getText,是否能够打印回文本?在onClick中调用setText据我所知,您只能在
onCreate()
中调用
setText()
一次。