這不是別人問的常見問題,我知道如何從EditText
得到價值。我正在爲我的應用程序創建一個登錄/註冊,我希望通過一個單獨的類/包檢索用戶登錄數據,並且用戶使用單獨的類/包註冊數據。注意:此類不是一項活動,但確實延伸AppCompatActivity
以允許使用findViewById
。如何從android中的另一個類的EditText中獲取值?
這是隻有一個get-Method
的UserSignUpData.class因爲它崩潰後
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()
開始和我打電話的onClick(signup)
的方法,我從這樣的
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
}
的MainActivity.class佈局聲明
所以我最終得到它指向
EditText et = (EditText) findViewById(R.id.signup_name);
一個NullPointerException
這是錯誤代碼:
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)
我也可能是誤以爲這是一個NullPointerException異常,但它的一些錯誤這就是從停止工作的代碼。謝謝
「是延伸AppCompatActivity允許使用findViewById的」 - 我不認爲你明白findViewById'如何'工作。就像那樣,你無法找到其他活動的觀點。 –