提交按鈕後我得到的錯誤eText.setError()致命錯誤
04-30 11:08:19.990 14339-14339/hr.tosapinaetfos.quizmaker E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at hr.tosapinaetfos.quizmaker.RegisterActivity.isEmpty(RegisterActivity.java:72)
at hr.tosapinaetfos.quizmaker.RegisterActivity.registerOnclick(RegisterActivity.java:58)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
這裏是代碼
public class RegisterActivity extends AppCompatActivity{
EditText username;
EditText password;
EditText passwordAgain;
Spinner accountTypeSpinner;
Button registerButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//Fetch registerbutton by id
Button registerButton = (Button) findViewById(R.id.registerButton);
//Fetch username, password,passwordAgain , accountTypeSpinner by id
EditText username = (EditText) findViewById(R.id.registerUsername);
EditText password = (EditText) findViewById(R.id.registerPassword);
EditText passwordAgain = (EditText) findViewById(R.id.registerPasswordAgain);
Spinner accountTypeSpinner = (Spinner) findViewById(R.id.registerSpinner);
}
public void registerOnclick(View v){
//Get text from objects
String strUsername = username.getText().toString();
String strPassword = password.getText().toString();
String strPassowrdAgain = passwordAgain.getText().toString();
String accounttype = accountTypeSpinner.getSelectedItem().toString();
if(isEmpty(strUsername,strPassword,strPassowrdAgain) == false){
Toast.makeText(this, "this is text", Toast.LENGTH_LONG).show();
}
}//End of registerOnclick
public Boolean isEmpty(String strUsername, String strPassword, String strPassowrdAgain){
Boolean isThereEmptyFields = false;
if (strUsername.length() == 0) {
Log.d("test", "before set error");
username.setError("First name is required!");
Log.d("test", "after set error");
isThereEmptyFields =true;
}
if (strPassword.length() == 0) {
password.setError("First name is required!");
isThereEmptyFields =true;
}
if (strPassowrdAgain.length() == 0) {
passwordAgain.setError("First name is required!");
isThereEmptyFields =true;
}
return isThereEmptyFields;
}
}
方法SETERROR()導致致命異常..如果我在函數registerOnclick(View v)中使用setError()它工作正常,但我需要創建新函數isEmpty()
我看不到'username'查布爾在'isEmpty()'裏面聲明。它是類範圍屬性還是什麼?你能提供完整的課程代碼嗎? –
@MaximBerezovsky更新 – Tomislav
移動'EditText username =(EditText)findViewById(R.id.registerUsername); EditText password =(EditText)findViewById(R.id.registerPassword); EditText passwordAgain =(EditText)findViewById(R.id.registerPasswordAgain);' 到'onCreate()'部分,以便它們在引用之前被初始化。 –