panda
panda

Reputation: 1344

How to display the int or char into a TextView

I want to process the comment input and write back into a TextView. I did a Class named PandaAssistents

package panda.com.db;

public class PandaAssistents{

    private static int CallOfIndex;
    private static String comment;
    public PandaAssistents(String input){
        this.comment = input;
        this.CallOfIndex=input.indexOf("熊貓");
    }

    public char getCall(){
        return comment.charAt(CallOfIndex);
    }
}

But when I field in a EditText and click a button, it generate an error.

private void btnAction(Button btn){
        btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                EditText edt = (EditText)IndexActivity.this.findViewById(R.id.EditText01);
                TextView txt = (TextView)IndexActivity.this.findViewById(R.id.TextView01);
                pa = new PandaAssistents(edt.getText().toString());
                txt.setText(pa.getCall());
            }
        });
    }

I don't know what the problem is

Upvotes: 0

Views: 432

Answers (2)

Bojin Li
Bojin Li

Reputation: 5799

Are you sure this.CallOfIndex=input.indexOf("熊貓") actually returns a valid index and not -1? Since you are not error checking this value, if the result is -1, when you call comment.charAt(CallOfIndex) with CallOfIndex being -1, you will get an IndexOutOfBoundsException

Upvotes: 0

jeet
jeet

Reputation: 29199

TextView or EditText's setText requires charsequence or resource id as input parameter, if you want to set char or int, convert to it string before setting into textview. so you can try following:

txt.setText(""+pa.getCall());

Upvotes: 4

Related Questions