I have a dialog that triggers on EditText's context menu. Dialog sets the amount of red, green and blue of EditText. The problem is that runtime error occurs when I click positive button. Items above works fine, they set the color of text in EditText as it should, but rgbDialog don't.
I think that the problem occurs here:
etRed = (EditText)findViewById(R.id.etRed);
etGreen = (EditText)findViewById(R.id.etGreen);
etBlue = (EditText)findViewById(R.id.etBlue);
But I don't know how to initialize etRed, etGreen and etBlue right. I think that I should place something before findView like
etRed = (EditText)Dialog.findViewById(R.id.etRed);
But don't know what, because everything is happening in the same method (onContextItemSelected)
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.white:
temp.setTextColor(Color.WHITE);
break;
case R.id.red:
temp.setTextColor(Color.RED);
break;
case R.id.green:
temp.setTextColor(Color.GREEN);
break;
case R.id.rgb:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("choose the amount of red green and blue");
LayoutInflater inflater = this.getLayoutInflater();
dialogBuilder.setView(inflater.inflate(R.layout.colors, null));
dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
etRed = (EditText)findViewById(R.id.etRed);
etGreen = (EditText)findViewById(R.id.etGreen);
etBlue = (EditText)findViewById(R.id.etBlue);
red=etRed.getText().toString();
green = etGreen.getText().toString();
blue = etBlue.getText().toString();
intRed = Integer.parseInt(red);
intGreen = Integer.parseInt(green);
intBlue = Integer.parseInt(blue);
temp.setTextColor(Color.rgb(intRed, intGreen, intBlue));
}
});
dialogBuilder.setNegativeButton("CANCEL", null);
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
break;
default:
break;
}
return super.onContextItemSelected(item);
}
EditText temp is set like this, and works fine for items above rgb
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context, menu);
temp = (EditText)findViewById(v.getId());
super.onCreateContextMenu(menu, v, menuInfo);
}
you can have something like below to call findViewById from correct layout:
View dialogView = inflater.inflate(R.layout.colors, null);
dialogBuilder.setView(dialogView );
and when you are calling findViewById in onClick you can have like:
etRed = (EditText)dialogView.findViewById(R.id.etRed);
etGreen = (EditText)dialogView.findViewById(R.id.etGreen);
etBlue = (EditText)dialogView.findViewById(R.id.etBlue);
Check if this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.