Java 随机变量导致的运行时错误
此代码正在生成以下错误Java 随机变量导致的运行时错误,java,android,eclipse,android-layout,android-intent,Java,Android,Eclipse,Android Layout,Android Intent,此代码正在生成以下错误 import java.util.Random; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.text.InputType; import android.view.Gravity; import android.view.View; import android.widget.Button; import and
import java.util.Random;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;
public class TextPlay extends Activity {
ToggleButton pasTog;
Button ChkCmd;
EditText input;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
ChkCmd = (Button) findViewById(R.id.Bresults);
pasTog = (ToggleButton) findViewById(R.id.tbPassword);
input = (EditText) findViewById(R.id.etCommands);
display = (TextView) findViewById(R.id.tvResults);
pasTog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (pasTog.isChecked()) {
input.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_PASSWORD);
} else {
input.setInputType(InputType.TYPE_CLASS_TEXT);
}
}
});
ChkCmd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String Check = input.getText().toString();
display.setText(Check);
if (Check.contentEquals("left")) {
display.setGravity(Gravity.LEFT);
} else if (Check.contentEquals("center")) {
display.setGravity(Gravity.CENTER);
} else if (Check.contentEquals("right")) {
display.setGravity(Gravity.RIGHT);
} else if (Check.contentEquals("blue")) {
display.setTextColor(Color.BLUE);
} else if (Check.contentEquals("WTF")) {
Random crazy = new Random();
display.setText("WTF!!!");
display.setTextSize(crazy.nextInt(75));
display.setText(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),
crazy.nextInt(265)));
switch (crazy.nextInt(3)) {
case 0:
display.setGravity(Gravity.LEFT);
break;
case 1:
display.setGravity(Gravity.CENTER);
break;
case 2:
display.setGravity(Gravity.RIGHT);
break;
}
} else {
display.setText("Invalid");
display.setGravity(Gravity.CENTER);
display.setTextColor(Color.BLACK);
}
}
});
}
}
正如您所看到的,返回类型是int
android.content.res.Resources$NotFoundException:字符串资源ID#0xffca961
改变这个
public static int rgb (int red, int green, int blue)
Added in API level 1
Return a color-int from red, green, blue components. The alpha component is implicity 255 (fully opaque). These component values should be [0..255], but there is no range check performed, so if they are out of range, the returned color is undefined.
Parameters
red Red component [0..255] of the color
green Green component [0..255] of the color
blue Blue component [0..255] of the color
到
setText(int)
查找具有所述id的资源,如果未找到,则获取ResourceNotFoundException
您需要的是setText(CharacterSequence)
编辑:
正如Chris Stratton所建议的,您使用setText
显示的次数超过两次。每次将新数据设置为Textview
如果要追加数据,请使用append
而不是setText
或者您可能打算将颜色设置为您想要的文本视图。虽然传递裸整数被错误地解释为资源ID是正确的,但根本不清楚其目的是将该颜色的值显示为可打印的数字,特别是调用setText()两次不可能完成任何有用的事情。@ChrisStratton right错过了它。也许op应该使用appender你是说调用setTextColor()而不是setText()?非常感谢!!克里斯斯特拉顿
public static int rgb (int red, int green, int blue)
Added in API level 1
Return a color-int from red, green, blue components. The alpha component is implicity 255 (fully opaque). These component values should be [0..255], but there is no range check performed, so if they are out of range, the returned color is undefined.
Parameters
red Red component [0..255] of the color
green Green component [0..255] of the color
blue Blue component [0..255] of the color
display.setText(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),crazy.nextInt(265)));
display.setText(String.valueOf(Color.rgb(crazy.nextInt(265), crazy.nextInt(265),crazy.nextInt(265))));