2017-02-16 98 views
-2

一切都很好?Android粘貼按鈕

我有一個問題,在用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

可供使用的不存在。你從來沒有宣佈過。 –

+0

@GabeSechan line 54 EditText txtNome =(EditText)findViewById(R.id.txtNome); –

+0

這是一個局部變量。錯誤的範圍。變量從未被聲明。 –

回答

1

EditText txtNome一個全局變量。