Android 安卓粘贴按钮
一切都好吗 在创建按钮时,我遇到了一个问题,该按钮的操作是在EditText中粘贴文本。我试过几种不同的方法,但都不管用。有人知道错误在哪里以及如何解决吗 错误:错误:(68,9)错误:找不到符号变量txtNomeAndroid 安卓粘贴按钮,android,Android,一切都好吗 在创建按钮时,我遇到了一个问题,该按钮的操作是在EditText中粘贴文本。我试过几种不同的方法,但都不管用。有人知道错误在哪里以及如何解决吗 错误:错误:(68,9)错误:找不到符号变量txtNome 公共类Pedidos扩展AppCompatActivity实现View.OnClickListener{ 私有静态最终整型请求对话框照片=1; 私人照片=0; 私人视图按钮面板; @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.on
公共类Pedidos扩展AppCompatActivity实现View.OnClickListener{
私有静态最终整型请求对话框照片=1;
私人照片=0;
私人视图按钮面板;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pedidos);
副极性();
}
私有无效复制极性(){
buttonColar=findViewById(R.id.buttonColar);
buttonColar.setOnClickListener(此);
}
公共void onClick(视图v){
开关(v.getId()){
案例R.id.buttonColar:ColardaaReadeTransferenceia();
打破
}
}
//转让协议的条款
私有无效ColardaaReadTransferenceia(){
ClipboardManager Colar=(ClipboardManager)getSystemService(Context.CLIPBOARD\u服务);
setText(Colar.getText());
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
按钮btn=(按钮)findViewById(R.id.btnCadastrar);
最终图像视图imgFoto=(图像视图)findViewById(R.id.imgFoto);
setOnClickListener(新视图.OnClickListener()){
@凌驾
公共void onClick(视图v){
setImageResource(android.R.color.transparent);
Intent camera=ImagePicker.getPickImageIntent(getBaseContext());
startActivityForResult(摄像头、请求对话框和照片);
}
});
btn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
EditText txtNome=(EditText)findviewbyd(R.id.txtNome);
视图按钮栏;
整数误差=0;
if(txtNome.getText().toString().equals(“”){
txtNome.setError(“FALTOU O NÚMERO DA O.S”);
txtNome.requestFocus();
误差=1;
}
如果(错误==0){
字符串URL=”http://meusite.com/insert_user.php";
字符串photoFile=“”;
试一试{
photoFile=getBaseContext().getPackageManager().getPackageInfo(getBaseContext().getPackageName(),0.applicationInfo.dataDir+“//photo//perfil.png”;
}捕获(PackageManager.NameNotFounde异常){
}
使用(getBaseContext())
.load(URL)
.setMultipartParameter(“nome_user”,txtNome.getText().toString())
.setMultipartFile(“照片用户”,新文件(照片文件))
.asJsonObject()
.setCallback(新的FutureCallback(){
@凌驾
未完成公共无效(异常e,JsonObject结果){
if(result.get(“no”).getAsString().equals(“YES”)){
Toast.makeText(getBaseContext(),“FOTO ENVIADA COM successo!”,Toast.LENGTH_LONG.show();
}
}
});
}
}
});
}
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
if(requestCode==请求对话框照片){
if(resultCode==Activity.RESULT\u确定){
位图photoUser=ImagePicker.getImageFromResult(getBaseContext(),resultCode,data);
ImageView imgFoto=(ImageView)findViewById(R.id.imgFoto);
设置图像位图(photoUser);
havePhoto=1;
//格拉瓦福托意大利面
文件diretorio=Environment.getDataDirectory();
字符串路径=“///data//”+getBaseContext().getPackageName()+“///photo//”;
diretorio=新文件(diretorio,路径);
diretorio.mkdirs();
OutputStream out=null;
File outputFile=新文件(diretorio,“perfil.png”);
试一试{
out=新文件outputstream(outputFile);
photoUser.compress(Bitmap.CompressFormat.PNG,100,out);
out.flush();
out.close();
}捕获(例外e){
}
}否则{
Toast.makeText(getBaseContext(),“Por-favor,selecione-uma-foto”,Toast.LENGTH_-LONG.show();
}
}
}
}
使编辑文本txtNome
成为全局变量。该变量不存在。您从未声明过它。@Gabeschen第54行EditText txtNome=(EditText)findViewById(R.id.txtNome);这是一个局部变量。错误的范围。变量从未声明过。@Gabeschen private EditText txtNome;我声明,错误消息已消失,但当我按下“粘贴”按钮时,应用程序将关闭。@gabeschen java.lang.NullPointerException:尝试调用虚拟方法“void android.widget.EditText.setText(java.lang.CharSequence)'在br.com.guilherme.gcmteste.Pedidos.ColardaaReadeTransferenceia(Pedidos.java:31)的br.com.guilherme.gcmteste.Pedidos.onClick(Pedidos.java:23)的空对象引用上
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();
}
}
}
}