1

SDKに慣れるために簡単なAndroidアプリケーションを作成しようとしていますが、setTextメソッドがTextViewを変更しない理由がわかりません。基本的に私がしたいのは、ユーザーが文字列を入力し、ユーザーに文字列で実行するメソッドを選択させ、出力を Textview に設定することですが、TextView を設定できないようです。ケースの後のスイッチで、そしてそれぞれのケースで、onCreateでメソッドを呼び出してみましたが、テキストを出力値に設定するものはありませんでした。私が間違っていることを誰かが知っていますか?

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


            }


}
4

2 に答える 2

1

私が間違っていることを誰かが知っていますか?

ではonCreate()、 を呼び出しsetText()ていますが、初期化していないため、TextView渡すパラメーターは です。nulloutput

コード内の他の場所で を呼び出していませんsetText()

于 2013-11-09T20:21:11.237 に答える
1

setTextまず、メソッドで呼び出された場所がなくOnclick、次にリスナーをボタンに設定していません

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


        }
于 2013-11-09T20:26:52.943 に答える