Android 如何在文本视图中显示int或char
我想处理注释输入并写回TextView。 我上了一堂名为PandaAsistents的课Android 如何在文本视图中显示int或char,android,android-edittext,textview,Android,Android Edittext,Textview,我想处理注释输入并写回TextView。 我上了一堂名为PandaAsistents的课 package panda.com.db; public class PandaAssistents{ private static int CallOfIndex; private static String comment; public PandaAssistents(String input){ this.comment = input;
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);
}
}
但当我在EditText中输入一个字段并单击一个按钮时,它会生成一个错误
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());
}
});
}
我不知道问题出在哪里,TextView或EditText的setText需要charsequence或resource id作为输入参数,如果要设置char或int,请在设置为TextView之前将其转换为string。因此,您可以尝试以下操作:
txt.setText(""+pa.getCall());
TextView或EditText的setText需要charsequence或resource id作为输入参数,如果要设置char或int,请在设置为TextView之前将其转换为string。因此,您可以尝试以下操作:
txt.setText(""+pa.getCall());
您确定
this.CallOfIndex=input.indexOf(“熊貓")
实际上返回一个有效的索引,而不是-1?因为您没有错误地检查这个值,如果结果是-1,当调用comment.charAt(CallOfIndex)
且CallOfIndex
为-1时,您将得到一个您确定this.CallOfIndex=input.indexOf(“熊貓“
实际上返回了一个有效的索引,而不是-1?因为您没有错误地检查这个值,如果结果是-1,当您调用comment.charAt(CallOfIndex)
且CallOfIndex
为-1时,您将得到一个尝试使用txt.setText(pa.getCall()+”);你能指定哪个错误吗?;Shashank Kadne:这是运行时错误,程序刚刚停止。请将LogCat中显示的错误发布给Hiral:可以。为什么?尝试使用txt.setText(pa.getCall()+);你能指定哪个错误吗?;Shashank Kadne:这是运行时错误,程序刚刚停止发布LogCat中显示的错误。Hiral:可以。为什么?