public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SeekBar HueSB = (SeekBar) findViewById(R.id.HueSB);
SeekBar SatSB = (SeekBar) findViewById(R.id.SatSB);
SeekBar ValueSB = (SeekBar) findViewById(R.id.ValueSB);
SeekBar RedSB = (SeekBar) findViewById(R.id.RedSB);
SeekBar BlueSB = (SeekBar) findViewById(R.id.BlueSB);
SeekBar GreenSB = (SeekBar) findViewById(R.id.GreenSB);
RedSB.setOnSeekBarChangeListener(new RGBChangeListener());
GreenSB.setOnSeekBarChangeListener(new RGBChangeListener());
BlueSB.setOnSeekBarChangeListener(new RGBChangeListener());
HueSB.setOnSeekBarChangeListener(new HSVChangeListener());
SatSB.setOnSeekBarChangeListener(new HSVChangeListener());
ValueSB.setOnSeekBarChangeListener(new HSVChangeListener());
}
private class HSVChangeListener implements OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
SeekBar RedSB = (SeekBar) findViewById(R.id.RedSB);
SeekBar BlueSB = (SeekBar) findViewById(R.id.BlueSB);
SeekBar GreenSB = (SeekBar) findViewById(R.id.GreenSB);
SeekBar HueSB = (SeekBar) findViewById(R.id.HueSB);
SeekBar SatSB = (SeekBar) findViewById(R.id.SatSB);
SeekBar ValueSB = (SeekBar) findViewById(R.id.ValueSB);
EditText RedET = (EditText) findViewById(R.id.RedET);
EditText GreenET = (EditText) findViewById(R.id.GreenET);
EditText BlueET = (EditText) findViewById(R.id.BlueET);
EditText HueET = (EditText) findViewById(R.id.HueET);
EditText SatET = (EditText) findViewById(R.id.SatET);
EditText ValET = (EditText) findViewById(R.id.ValueET);
TextView tvColor = (TextView) findViewById(R.id.color1TV);
float[] hsv = new float[3];
if(fromUser == true) {
if (seekBar.equals(HueSB)) {
HueET.setText(""+progress);
hsv[0] = Float.parseFloat(HueET.getText().toString());
hsv[1] = Float.parseFloat(SatET.getText().toString());
hsv[2] = Float.parseFloat(ValET.getText().toString());
int color = Color.HSVToColor(hsv);
RedSB.setProgress(Color.red(color));
BlueSB.setProgress(Color.blue(color));
GreenSB.setProgress(Color.green(color));
RedET.setText(""+Color.red(color));
BlueET.setText(""+Color.blue(color));
GreenET.setText(""+Color.green(color));
tvColor.setBackgroundColor(color);
} else if (seekBar.equals(SatSB)) {
SatET.setText(String.format("%.2f", (progress * .01f)));
hsv[0] = Float.parseFloat(HueET.getText().toString());
hsv[1] = Float.parseFloat(SatET.getText().toString());
hsv[2] = Float.parseFloat(ValET.getText().toString());
int color = Color.HSVToColor(hsv);
RedSB.setProgress(Color.red(color));
BlueSB.setProgress(Color.blue(color));
GreenSB.setProgress(Color.green(color));
RedET.setText(""+Color.red(color));
BlueET.setText(""+Color.blue(color));
GreenET.setText(""+Color.green(color));
tvColor.setBackgroundColor(color);
} else if (seekBar.equals(ValueSB)) {
ValET.setText(String.format("%.2f", (progress * .01f)));
hsv[0] = Float.parseFloat(HueET.getText().toString());
hsv[1] = Float.parseFloat(SatET.getText().toString());
hsv[2] = Float.parseFloat(ValET.getText().toString());
int color = Color.HSVToColor(hsv);
RedSB.setProgress(Color.red(color));
BlueSB.setProgress(Color.blue(color));
GreenSB.setProgress(Color.green(color));
RedET.setText(""+Color.red(color));
BlueET.setText(""+Color.blue(color));
GreenET.setText(""+Color.green(color));
tvColor.setBackgroundColor(color);
}
}
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
}
private class RGBChangeListener implements OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
SeekBar RedSB = (SeekBar) findViewById(R.id.RedSB);
SeekBar BlueSB = (SeekBar) findViewById(R.id.BlueSB);
SeekBar GreenSB = (SeekBar) findViewById(R.id.GreenSB);
SeekBar HueSB = (SeekBar) findViewById(R.id.HueSB);
SeekBar SatSB = (SeekBar) findViewById(R.id.SatSB);
SeekBar ValueSB = (SeekBar) findViewById(R.id.ValueSB);
EditText RedET = (EditText) findViewById(R.id.RedET);
EditText GreenET = (EditText) findViewById(R.id.GreenET);
EditText BlueET = (EditText) findViewById(R.id.BlueET);
EditText HueET = (EditText) findViewById(R.id.HueET);
EditText SatET = (EditText) findViewById(R.id.SatET);
EditText ValET = (EditText) findViewById(R.id.ValueET);
TextView tvColor = (TextView) findViewById(R.id.color1TV);
float[] hsv = new float[3];
if(fromUser == true) {
if(seekBar.equals(RedSB)) {
RedET.setText(""+progress);
int color = Color.rgb(Integer.parseInt(RedET.getText().toString()),Integer.parseInt(GreenET.getText().toString()), Integer.parseInt(BlueET.getText().toString()));
Color.colorToHSV(color, hsv);
HueSB.setProgress((int)hsv[0]);
SatSB.setProgress((int)(hsv[1]*100));
ValueSB.setProgress((int)(hsv[2]*100));
HueET.setText(""+String.format("%.0f",hsv[0]));
SatET.setText(""+String.format("%.0f",hsv[1]));
ValET.setText(""+String.format("%.0f",hsv[2]));
tvColor.setBackgroundColor(color);;
} else if (seekBar.equals(BlueSB)) {
BlueET.setText(""+progress);
int color = Color.rgb(Integer.parseInt(RedET.getText().toString()),Integer.parseInt(GreenET.getText().toString()), Integer.parseInt(BlueET.getText().toString()));
Color.colorToHSV(color, hsv);
HueSB.setProgress((int)hsv[0]);
SatSB.setProgress((int)(hsv[1]*100));
ValueSB.setProgress((int)(hsv[2]*100));
HueET.setText(""+String.format("%.0f",hsv[0]));
SatET.setText(""+String.format("%.0f",hsv[1]));
ValET.setText(""+String.format("%.0f",hsv[2]));
tvColor.setBackgroundColor(color);
} else if (seekBar.equals(GreenSB)) {
GreenET.setText(""+progress);
int color = Color.rgb(Integer.parseInt(RedET.getText().toString()),Integer.parseInt(GreenET.getText().toString()), Integer.parseInt(BlueET.getText().toString()));
Color.colorToHSV(color, hsv);
HueSB.setProgress((int)hsv[0]);
SatSB.setProgress((int)(hsv[1]*100));
ValueSB.setProgress((int)(hsv[2]*100));
HueET.setText(""+String.format("%.0f",hsv[0]));
SatET.setText(""+String.format("%.0f",hsv[1]));
ValET.setText(""+String.format("%.0f",hsv[2]));
tvColor.setBackgroundColor(color);
}
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
}
我在RGB和HSV搜索栏中都出现此错误
Logcat错误:
HSV SeekBar错误
04-30 16:26:19.913: E/InputEventReceiver(2094): Exception dispatching input event.
04-30 16:26:19.953: E/AndroidRuntime(2094): FATAL EXCEPTION: main
04-30 16:26:19.953: E/AndroidRuntime(2094): Process: cs2302.dabrams1.rgbhsvchooser, PID: 2094
04-30 16:26:19.953: E/AndroidRuntime(2094): java.lang.NumberFormatException: Invalid float: ""
04-30 16:26:19.953: E/AndroidRuntime(2094): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
04-30 16:26:19.953: E/AndroidRuntime(2094): at java.lang.StringToReal.parseFloat(StringToReal.java:289)
04-30 16:26:19.953: E/AndroidRuntime(2094): at java.lang.Float.parseFloat(Float.java:300)
04-30 16:26:19.953: E/AndroidRuntime(2094): at cs2302.dabrams1.rgbhsvchooser.MainActivity$HSVChangeListener.onProgressChanged(MainActivity.java:81)
RGB SeekBar错误
04-30 16:26:29.373: E/AndroidRuntime(2114): FATAL EXCEPTION: main
04-30 16:26:29.373: E/AndroidRuntime(2114): Process: cs2302.dabrams1.rgbhsvchooser, PID: 2114
04-30 16:26:29.373: E/AndroidRuntime(2114): java.lang.NumberFormatException: Invalid int: ""
04-30 16:26:29.373: E/AndroidRuntime(2114): at java.lang.Integer.invalidInt(Integer.java:137)
04-30 16:26:29.373: E/AndroidRuntime(2114): at java.lang.Integer.parseInt(Integer.java:358)
04-30 16:26:29.373: E/AndroidRuntime(2114): at java.lang.Integer.parseInt(Integer.java:331)
04-30 16:26:29.373: E/AndroidRuntime(2114): at cs2302.dabrams1.rgbhsvchooser.MainActivity$RGBChangeListener.onProgressChanged(MainActivity.java:170)
04-30 16:26:29.373: E/AndroidRuntime(2114): at android.widget.SeekBar.onProgressRefresh(SeekBar.java:91)
我不确定如何解决问题。这是格式化问题。
答案 0 :(得分:0)
问题在于,当您在听众处呼叫Integer.parseInt
或Float.parseFloat
时,他们的论点有时只是空字符串 - ""
。如果您第一次进入监听器并且seekBar.equals(BlueSB) == true
,例如,该怎么办?在这种情况下,RedET
和GreenET
尚无任何价值。解决方案非常简单:将一些默认值(例如0
)添加到EditTexts
布局中的所有XML
。