2016-04-30 161 views
0

提交按鈕後我得到的錯誤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()

+0

我看不到'username'查布爾在'isEmpty()'裏面聲明。它是類範圍屬性還是什麼?你能提供完整的課程代碼嗎? –

+0

@MaximBerezovsky更新 – Tomislav

+0

移動'EditText username =(EditText)findViewById(R.id.registerUsername); EditText password =(EditText)findViewById(R.id.registerPassword); EditText passwordAgain =(EditText)findViewById(R.id.registerPasswordAgain);' 到'onCreate()'部分,以便它們在引用之前被初始化。 –

回答

0
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_register); 
Button registerButton = (Button) findViewById(R.id.registerButton); 
//Fetch username, password,passwordAgain , accountTypeSpinner by id 
username = (EditText) findViewById(R.id.registerUsername); 
password = (EditText) findViewById(R.id.registerPassword); 
passwordAgain = (EditText) findViewById(R.id.registerPasswordAgain); 
accountTypeSpinner = (Spinner) findViewById(R.id.registerSpinner); 
} 

public void registerOnclick(View v){ 

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)){ 
    Toast.makeText(this, "this is text", Toast.LENGTH_LONG).show(); 

} } 

你使用,如果條件格式錯誤使用,而不是

if(!isEmpty(strUsername,strPassword,strPassowrdAgain)) 

if(isEmpty(strUsername,strPassword,strPassowrdAgain) == false) 
+0

確定,但在這裏不是問題 – Tomislav

+0

檢查更新答案 – sasikumar

+0

錯誤發生顯示java.lang.NullPointerException 在hr.tosapinaetfos.quizmaker.RegisterActivity.isEmpty – sasikumar

0
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; 
} 

}

+0

然後我得到新的致命錯誤,然後我註冊活動加載致命例外:主 java.lang.RuntimeException:無法實例化活動ComponentInfo {小時。 tosapinaetfos.quizmaker/hr.tosapinaetfos.quizmaker.RegisterActivity}:java.lang.NullPointerException – Tomislav

+0

你能更新你的完整代碼嗎? –

+0

我想看到完整的活動類代碼,因爲您顯示的錯誤不在此代碼 –