我很困惑这个错误背后可能是什么原因。在我输入一个数字之后使我的edittext字段为null我的应用程序正在崩溃。你找到了解决这种情况的方法吗?
这是我的代码
java文件
public class CSLActivity extends AppCompatActivity {
private Common mApp;
private AutoCompleteTextView clgName, conCent, loc;
private MultiAutoCompleteTextView skill;
private String[] qulify = new String[]{"Select Highest Qualification", "PG", "UG", "DIPLOMA"};
public String abc, abcd;
public ScrollView scrollView;
public ProgressBar progressBar;
private String q;
private Spinner level;
private int ab = 1990;
public EditText fullName, frm_yr, to_yr;
private boolean ins = false;
private boolean cIns = false;
private Button next;
private TextfieldValidator textfieldValidator;
private AlphabetValidator alphabetValidator;
private int i = 0, j = 0;
private boolean exit = false;
private String get;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_csl);
scrollView = (ScrollView) findViewById(R.id.scroll_view);
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
fullName = (EditText) findViewById(R.id.fullName);
level = (Spinner) findViewById(R.id.level);
clgName = (AutoCompleteTextView) findViewById(R.id.clg_name);
conCent = (AutoCompleteTextView) findViewById(R.id.concent);
frm_yr = (EditText) findViewById(R.id.frm_yr);
to_yr = (EditText) findViewById(R.id.to_yr);
skill = (MultiAutoCompleteTextView) findViewById(R.id.skill);
loc = (AutoCompleteTextView) findViewById(R.id.location);
textfieldValidator = new TextfieldValidator();
alphabetValidator = new AlphabetValidator();
next = (Button) findViewById(R.id.next);
frm_yr.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//abc = frm_yr.getText().toString();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
//i = Integer.parseInt(frm_yr.getText().toString());
//i = Integer.parseInt(abc.toString());
i = Integer.parseInt(frm_yr.getText().toString());
}
});
to_yr.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
//j = Integer.parseInt(to_yr.getText().toString());
//j = Integer.parseInt(abcd.toString());
j = Integer.parseInt(to_yr.getText().toString());
}
});
new GetCollege(CSLActivity.this, progressBar, scrollView, clgName).execute();
new GetCourse(CSLActivity.this, conCent).execute();
new GetSkillSet(CSLActivity.this, scrollView, progressBar, skill).execute();
new GetLocation(CSLActivity.this, scrollView, loc, progressBar).execute();
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_item, qulify);
level.setAdapter(adapter1);
level.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
get = level.getSelectedItem().toString();
if (get.equalsIgnoreCase("PG")) {
q = "1";
} else if (get.equalsIgnoreCase("UG")) {
q = "2";
} else {
q = "3";
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
clgName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView txt = (TextView) view.findViewById(R.id.ins_name);
clgName.setText(txt.getText().toString());
conCent.requestFocus();
ins = true;
}
});
conCent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView txt = (TextView) view.findViewById(R.id.display);
conCent.setText(txt.getText().toString());
cIns = true;
frm_yr.requestFocus();
}
});
loc.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView txt1 = (TextView) view.findViewById(R.id.ins_name);
loc.setText(txt1.getText().toString());
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromInputMethod(view.getWindowToken(), 0);
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.next:
if (fullName.getText().toString().length() == 0) {
fullName.setError("Field Mandatory");
fullName.requestFocus();
} else if (!alphabetValidator.validate(fullName.getText().toString())) {
fullName.setError("Enter a Valid Name");
fullName.requestFocus();
} else if (get.equalsIgnoreCase("Select Highest Qualification")) {
level.requestFocus();
Toast.makeText(getApplicationContext(), "select Qualification", Toast.LENGTH_SHORT).show();
} else if (clgName.getText().toString().length() == 0) {
clgName.setError("Field Mandatory");
clgName.requestFocus();
} else if (conCent.getText().toString().length() == 0) {
conCent.setError("Field Mandatory");
conCent.requestFocus();
} else if (frm_yr.getText().toString().length() == 0) {
frm_yr.setError("Field Mandatory");
frm_yr.requestFocus();
} else if (to_yr.getText().toString().length() == 0) {
to_yr.setError("Field Mandatory");
to_yr.requestFocus();
} else if (i > j) {
to_yr.setError("Passed out year less than join year");
to_yr.requestFocus();
} else if (i == j) {
to_yr.setError("Check the year entered");
to_yr.requestFocus();
} else if (i <= ab) {
frm_yr.setError("Enter a valid Year");
frm_yr.requestFocus();
} else if (j <= ab) {
to_yr.setError("Enter a valid Year");
to_yr.requestFocus();
} else if (skill.getText().toString().length() == 0) {
skill.setError("Field Mandatory");
skill.requestFocus();
} else if (!textfieldValidator.validate(skill.getText().toString())) {
skill.setError("Enter a Valid Skill");
skill.requestFocus();
} else if (loc.getText().toString().length() == 0) {
loc.setError("Field Mandatory");
loc.requestFocus();
} else if (!textfieldValidator.validate(loc.getText().toString())) {
loc.setError("Enter a Valid Location");
} else {
next.setEnabled(false);
new CslIns(CSLActivity.this, mApp.getPreference().getString(Common.u_id, ""), fullName.getText().toString(), q,
clgName.getText().toString(), conCent.getText().toString(), frm_yr.getText().toString(), to_yr.getText().toString(),
skill.getText().toString(), loc.getText().toString(), ins, cIns).execute();
/*startActivity(new Intent(getApplicationContext(), MailVerify.class));
finish();*/
}
break;
}
}
});
}
@Override
public void onBackPressed() {
if (exit) {
mApp.getPreference().edit().putBoolean(Common.PAGE1, false).commit();
super.onBackPressed();
return;
} else {
Toast.makeText(this, "Press Back again to Cancel Signup Process.", Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 2000);
}
}
}
我的xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.shuan.Project.signup.employee.CSLActivity">
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/activity_horizontal_margin"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/fullName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/standard_margin"
android:hint="Full Name"
android:imeOptions="actionNext"
android:inputType="textPersonName" />
</android.support.design.widget.TextInputLayout>
<Spinner
android:id="@+id/level"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/standard_margin" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/standard_margin">
<AutoCompleteTextView
android:id="@+id/clg_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/clg_name"
android:imeOptions="actionNext" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/standard_margin">
<AutoCompleteTextView
android:id="@+id/concent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/concent"
android:imeOptions="actionNext"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:id="@+id/yr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/standard_margin"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Joined"
android:textColor="#000"
android:textStyle="bold" />
<EditText
android:id="@+id/frm_yr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/small_margin"
android:hint="Year"
android:imeOptions="actionNext"
android:inputType="numberDecimal"
android:maxLength="4"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Passed :"
android:textColor="#000"
android:textStyle="bold" />
<EditText
android:id="@+id/to_yr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/small_margin"
android:hint="Year"
android:imeOptions="actionNext"
android:inputType="numberDecimal"
android:maxLength="4"
android:singleLine="true" />
</LinearLayout>
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/layout_skill"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/standard_margin">
<MultiAutoCompleteTextView
android:id="@+id/skill"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Your Skills"
android:imeOptions="actionNext"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/layout_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/standard_margin">
<AutoCompleteTextView
android:id="@+id/location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/location"
android:imeOptions="actionDone"
android:inputType="textAutoComplete" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/next"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:background="@drawable/signin_border"
android:text="START"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
log cat
FATAL EXCEPTION: main
Process: com.shuan.Project, PID: 1432
java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:334)
at com.shuan.Project.signup.employee.CSLActivity$1.afterTextChanged(CSLActivity.java:95)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7998)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:9814)
at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:990)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:529)
at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:224)
at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:38)
at android.view.inputmethod.BaseInputConnection.deleteSurroundingText(BaseInputConnection.java:252)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:389)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5649)
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:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
答案 0 :(得分:0)
初看起来,对我来说它似乎阻止onAfterTextChanged,因为它试图从一个空的String解析一个int,所以它只是抛出错误。 在解析整数之前尝试验证EditText是否为“”,如果它不是“”或null作为你的代码,如果它是“”或null给它一个默认值,如0或你想要的。
答案 1 :(得分:0)
这次崩溃是因为你在afterTextChanged回调中的代码。 在将其解析为整数之前,您应该检查to_yr.getText是否具有某个有效值。不要解析空字符串。
使用以下代码:
public void afterTextChanged(Editable s) {
if(!TextUtils.isEmpty(to_yr.getText()){
j = Integer.parseInt(to_yr.getText().toString());
}
}
答案 2 :(得分:0)
试试此代码
public void afterTextChanged(Editable s) {
if(to_yr.getText().toString().length != 0){
j =Integer.parseInt(to_yr.getText().toString());
}
}
它适用于我
答案 3 :(得分:0)
这种情况正在发生,因为您解析空字符串并且 Integer 类抛出了名为:
的异常无效的int
java.lang.NumberFormatException
在解析整数之前进行检查。例如
<ScrollSync>
<AutoSizer disableHeight>
<div>
<Grid className={styles.HeaderGrid} /> {/* hour intervals */}
<div className={styles.Body}>
<Grid /> {/* table striped background */}
<Collection/> {/* courier routes has to be collection */}
<Collection/> {/* courier working hours has to be collection */}
</div>
</div>
</AutoSizer>
</ScrollSync>