[英]App crashes due to TextInputlayout declaration
I am new to android i am developing and application where the app crashes when i click the fab button to open new activity.Button is working fine but due to first 3 lines of code app is crashing as i want to validated the form i used setError for TextInputLayout but the due to declaration of TextInputLayout the app crashes... 我是新来的android我正在开发和应用程序,当我单击fab按钮以打开新的activity时应用程序崩溃。按钮运行正常,但由于前3行代码应用程序崩溃,因为我想验证我使用的setError setError对于TextInputLayout,但由于TextInputLayout的声明导致应用崩溃...
final TextInputLayout name12=(TextInputLayout)findViewById(R.id.NameWrapper);
final TextInputLayout payment12=(TextInputLayout)findViewById(R.id.PaymentWrapper);
final TextInputLayout leave12=(TextInputLayout)findViewById(R.id.LeaveWrapper);
final EditText Name1 = (EditText) findViewById(R.id.editText2);
final EditText PhoneNo = (EditText) findViewById(R.id.editText3);
final EditText JobDesc = (EditText) findViewById(R.id.editText4);
final EditText Payment = (EditText) findViewById(R.id.editText5);
final EditText Leave = (EditText) findViewById(R.id.editText6);
final EditText Adharno = (EditText) findViewById(R.id.editText7);
final TextView Start = (TextView) findViewById(R.id.textView);
final TextView End = (TextView) findViewById(R.id.textView2);
final RadioButton Daily = (RadioButton) findViewById(R.id.Rb1);
final RadioButton Monthly = (RadioButton) findViewById(R.id.Rb2);
final Button AddEmp=(Button)findViewById(R.id.b1);
final Button Cancel=(Button)findViewById(R.id.b2);
final RadioGroup myRadiogeoup=(RadioGroup)findViewById(Rbg);
//Add Employee Btn
AddEmp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
String name1=Name1.getText().toString();
String phone1=PhoneNo.getText().toString();
String jobdesc=JobDesc.getText().toString();
String payment=Payment.getText().toString();
String leave=Leave.getText().toString();
String ahdarno=Adharno.getText().toString();
int id=myRadiogeoup.getCheckedRadioButtonId();
final RadioButton mySelectedRadioButton=(RadioButton)findViewById(id);
String paymethod=mySelectedRadioButton.getText().toString();
String startd=Start.getText().toString();
String end=End.getText().toString();
//validation and data insertion
if (name12.getEditText().length() == 0) {
name12.setError("Enter Name");
name12.setErrorEnabled(true);
}
else{
name12.setErrorEnabled(false);
}
if (leave12.getEditText().length() == 0) {
leave12.setError("Enter date");
leave12.setErrorEnabled(true);
}
else
{
leave12.setErrorEnabled(false);
}
if (payment12.getEditText().length() == 0) {
payment12.setError("Enter Password");
payment12.setErrorEnabled(true);
}
else{
payment12.setErrorEnabled(false);
}
if(Start.getText().length()==0)
{
Toast.makeText(getApplicationContext(),"Enter Start Date",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(),"Data Entered",Toast.LENGTH_SHORT).show();
}
}
});
please help if anyone knows any other way of validation. 如果有人知道其他任何验证方式,请提供帮助。 sorry for my wrong code or if the method is wrong cuz i am new to android
对不起,我的代码错误或方法错误,因为我是Android新手
--logcat --logcat
24 12:11:06.725 20230-20230/com.navinkadlak.typroject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.navinkadlak.typroject, PID: 20230
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.navinkadlak.typroject/com.navinkadlak.typroject.addemployee}: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.design.widget.TextInputLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2555)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2629)
at android.app.ActivityThread.access$800(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5706)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1033)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.design.widget.TextInputLayout
at com.navinkadlak.typroject.addemployee.onCreate(addemployee.java:33)
at android.app.Activity.performCreate(Activity.java:6092)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2502)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2629)
at android.app.ActivityThread.access$800(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5706)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1033)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
final TextInputLayout name12=(TextInputLayout)findViewById(R.id.NameWrapper);
final TextInputLayout payment12=(TextInputLayout)findViewById(R.id.PaymentWrapper);
final TextInputLayout leave12=(TextInputLayout)findViewById(R.id.LeaveWrapper);
Compare R.id.NameWrapper,R.id.PaymentWrapper, R.id.LeaveWrapper with xlm. 比较R.id.NameWrapper,R.id.PaymentWrapper,R.id.LeaveWrapper与xlm。 One of them refer to linearlayout so its not cast into textinputlayout.
其中之一引用linearlayout,因此不会将其转换为textinputlayout。
Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.design.widget.TextInputLayout
tells you exactly what happened. Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.design.widget.TextInputLayout
可以准确地告诉您发生了什么。
You are trying to cast some View
to TextInputLayout
but it is actually the LinearLayout
. 您试图将一些
View
TextInputLayout
为TextInputLayout
但实际上是LinearLayout
。
So one of: 因此,其中之一:
R.id.NameWrapper
R.id.PaymentWrapper
R.id.LeaveWrapper;
Is Linear Layout actually. 实际上是线性布局。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.