Sabbib Sabbib - 4 months ago 11
Android Question

How to get values from EditText from a different Class in android?

This is not the usual question everyone else has asked, I know how to get the value from EditText. I am creating a login/signup for my app, I want to have the user login data retrieved by one separate class/package and the user sign up data with a separate class/package. NOTE: this Class is not an activity but does extend AppCompatActivity to allow the use of findViewById.

This is the UserSignUpData.class with just one get-Method as it crashes start after

public class UserSignUpData extends AppCompatActivity {

public String getSignUpFullName() {
EditText et = (EditText) findViewById(R.id.signup_name); //NullPointerException
String fn = et.getText().toString();
return fn;
}//end getSignUpFullName()


And I am calling the onClick(signup) method that I declared in the layout from the MainActivity.class like this

public class MainActivity extends AppCompatActivity {
public void signUp(View v){
UserSignUpData signUpData = new UserSignUpData();
String name = signUpData.getSignUpFullName();
Toast.makeText(MainActivity.this, ud.getSignUpFullName(), Toast.LENGTH_SHORT).show();
//just to test if it works
}


So I end up getting a NullPointerException which points to

EditText et = (EditText) findViewById(R.id.signup_name);


this is the error code

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:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
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:525)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4240) 
at android.view.View$PerformClick.run(View.java:17721) 
at android.os.Handler.handleCallback(Handler.java:730) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5103) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:72)
at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:193)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:183)
at com.example.pc_charlie.loginapp.UserSignUpData.getSignUpFullName(UserSignUpData.java:12)
at com.example.pc_charlie.loginapp.MainActivity.signUp(MainActivity.java:52)
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:4240) 
at android.view.View$PerformClick.run(View.java:17721) 
at android.os.Handler.handleCallback(Handler.java:730) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5103) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 


I could also be wrong about it being a NullPointerException, but its some error thats stopping the code from working. thanks

Answer

If the class is not for an Activity. You should not extend activity.

I'm assuming you are trying to separate out the login into another class.

In that case just pass the EditText as a parameter to the method getSignUpFullName as getSignUpFullName(EditText et) after instantiating it properly in the MainActivity. Do not try to find it in the UserSignUpData class.