[英]casting 'findViewByld(...)' to 'EditText' is redundant
在行txtPass=(EditText) findViewById(R.id.txtPass);
它說將 'findViewByld(...)' 轉換為 'EditText' 是多余的。 我該如何解決?
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class datainsert extends AppCompatActivity {
EditText txtPass, txtName, txtNum, txtEmail;
Button btnLogin, btnSignUp;
DatabaseReference reff;
Member member;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
txtPass=(EditText) findViewById(R.id.txtPass);
txtName=(EditText)findViewById(R.id.txtName);
txtNum=(EditText)findViewById(R.id.txtNum);
txtEmail=(EditText)findViewById(R.id.txtEmail);
btnLogin=(Button)findViewById(R.id.btnLogin);
btnSignUp=(Button)findViewById(R.id.btnSignUp);
reff= FirebaseDatabase.getInstance().getReference().child("Member");
member=new Member();
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Float agea=Float.parseFloat(txtPass.getText().toString().trim());
Float hit= Float.parseFloat(txtName.getText().toString().trim());
Float dsa= Float.parseFloat(txtEmail.getText().toString().trim());
Long phn=Long.parseLong(txtNum.getText().toString().trim());
member.setName(txtName.getText().toString().trim());
member.setEmail(txtEmail.getText().toString().trim());
member.setHt(dsa);
member.setHm(hit);
member.setPh(phn);
member.setHt(agea);
reff.push().setValue(member);
Toast.makeText(datainsert.this,"data inserted sucessfully",Toast.LENGTH_LONG).show();
}
});
}
}```
只需刪除演員表即可刪除警告:
txtPass = findViewById(R.id.txtPass);
Java 從findViewById
找出了您想要的內容,因此無需findViewById
轉換。 該消息並不是真正的錯誤(現有代碼將編譯並運行得很好),因此保留它沒有任何害處。刪除冗余被認為是更好的風格。
替換這些行
txtPass=(EditText) findViewById(R.id.txtPass);
txtName=(EditText)findViewById(R.id.txtName);
txtNum=(EditText)findViewById(R.id.txtNum);
txtEmail=(EditText)findViewById(R.id.txtEmail);
用這些
txtPass=findViewById(R.id.txtPass);
txtName=findViewById(R.id.txtName);
txtNum=findViewById(R.id.txtNum);
txtEmail=findViewById(R.id.txtEmail);
視圖類型和實際視圖都相同,即 EditText,因此在這種情況下不需要類型轉換,您可以直接分配值
要更多地了解 TypeCasting 檢查這個為什么我們總是在 Android/Java 中進行類型轉換?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.