在创建一个带有在EditText中粘贴文本的操作的按钮时,我遇到了问题。我尝试了几种不同的方法,但它没有用。有没有人知道错误在哪里以及如何解决它?
错误:错误:(68,9)错误:找不到符号变量txtNome
public class Pedidos extends AppCompatActivity implements View.OnClickListener {
private static final int REQUEST_DIALOG_PHOTO = 1;
private int havePhoto = 0;
private View buttonColar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pedidos);
CopiarColar();
}
private void CopiarColar() {
buttonColar = findViewById(R.id.buttonColar);
buttonColar.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.buttonColar: colarDaAreaDeTransferencia();
break;
}
}
//Cola o texto que estava na área de transferência
private void colarDaAreaDeTransferencia() {
ClipboardManager Colar = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
txtNome.setText(Colar.getText());
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button btn = (Button)findViewById(R.id.btnCadastrar);
final ImageView imgFoto = (ImageView)findViewById(R.id.imgFoto);
imgFoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imgFoto.setImageResource(android.R.color.transparent);
Intent camera = ImagePicker.getPickImageIntent(getBaseContext());
startActivityForResult(camera, REQUEST_DIALOG_PHOTO);
}
});
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText txtNome = (EditText)findViewById(R.id.txtNome);
View buttonColar;
int error = 0;
if (txtNome.getText().toString().equals("")){
txtNome.setError("FALTOU O NÚMERO DA O.S");
txtNome.requestFocus();
error = 1;
}
if (error == 0) {
String URL = "http://meusite.com/insert_user.php";
String photoFile = "";
try {
photoFile = getBaseContext().getPackageManager().getPackageInfo(getBaseContext().getPackageName(), 0).applicationInfo.dataDir + "//photo//perfil.png";
} catch (PackageManager.NameNotFoundException e) {
}
Ion.with(getBaseContext())
.load(URL)
.setMultipartParameter("nome_user", txtNome.getText().toString())
.setMultipartFile("photo_user", new File(photoFile))
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
if (result.get("retorno").getAsString().equals("YES")) {
Toast.makeText(getBaseContext(), "FOTO ENVIADA COM SUCESSO!", Toast.LENGTH_LONG).show();
}
}
});
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == REQUEST_DIALOG_PHOTO){
if (resultCode == Activity.RESULT_OK){
Bitmap photoUser = ImagePicker.getImageFromResult(getBaseContext(), resultCode, data);
ImageView imgFoto = (ImageView)findViewById(R.id.imgFoto);
imgFoto.setImageBitmap(photoUser);
havePhoto = 1;
// Grava foto pasta
File diretorio = Environment.getDataDirectory();
String path = "//data//" + getBaseContext().getPackageName() + "//photo//";
diretorio = new File(diretorio, path);
diretorio.mkdirs();
OutputStream out = null;
File outputFile = new File(diretorio, "perfil.png");
try {
out = new FileOutputStream(outputFile);
photoUser.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
}
} else {
Toast.makeText(getBaseContext(), "Por favor, selecione uma foto", Toast.LENGTH_LONG).show();
}
}
}
}
答案 0 :(得分:1)
使EditText txtNome
成为全局变量。