简体   繁体   中英

Android setText method doesn't set text in TextView

I'm trying to write a simple android application to familiarize myself with the sdk and I can't figure out why the setText method isn't changing the TextView. Basically what I want to happen is for the user to input a string, have the user pick which method to run on the string and set the output to a Textview but I can't seem to set the TextView. I have tried calling the method in the onCreate, in the switch after the cases and in each case but none set the text to the output value. Does anyone know what I am doing wrong?

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


            }


}

Does anyone know what I am doing wrong?

In onCreate() , you are calling setText() on the TextView , but the parameter you are passing is null , as you have not initialized output .

Nowhere else in your code are you calling setText() .

Firstly you have nowhere called setText in Onclick method and secondly you haven't set Listeners to the buttons

1) Set onClickListners to your button

 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 here in the onClick method :

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


        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM