简体   繁体   English

从EditText获得价值的问题

[英]Problems with getting value from EditText

I am popping up a dialog box where user enters the username and password. 我弹出一个对话框,用户在其中输入用户名和密码。 For testing purposes i want to give an alert message containing the username that the user entered in an EditText when he/she clicks Login. 为了进行测试,我想给出一条警告消息,其中包含用户单击“登录”时在EditText中输入的用户名。 However my app crashes whenever login is pressed. 但是,每按一次登录,我的应用程序就会崩溃。 Note: The app doesn't crash if i remove the EditText and just popup a toast message upon clicking Login. 注意:如果我删除EditText并单击“登录”后仅弹出一条敬酒消息,则该应用程序不会崩溃。 Here is my implementation: 这是我的实现:

public Dialog onCreateDialog(Bundle savedInstanceState){
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        //get the layout inflater
        final LinearLayout loginLayout = (LinearLayout) findViewById(R.id.popup_signin);

        LayoutInflater inflater = getActivity().getLayoutInflater();

        builder.setView(inflater.inflate(R.layout.popup_signin, null))

                .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int d){
                        //sign in the user...
                        String username = ((EditText) loginLayout.findViewById(R.id.username)).getText().toString();
                        //String password = ((EditText) findViewById(R.id.password)).getText().toString();
                        Toast.makeText(getApplicationContext(), "Hello World "+username, Toast.LENGTH_LONG).show();
                       // new PostLogin(username,password).execute();

                    }

popup_signin.xml popup_signin.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_signin"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FFFFFF">

<EditText
    android:id="@+id/username"
    android:inputType="text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="4dp"
    android:typeface="monospace"
    android:hint="enter your ID"/>

</LinearLayout>

LogCat LogCat

07-08 01:03:15.812: E/AndroidRuntime(28048): FATAL EXCEPTION: main
07-08 01:03:15.812: E/AndroidRuntime(28048): java.lang.NullPointerException
07-08 01:03:15.812: E/AndroidRuntime(28048):    at com.example.hkuapp.MainActivity$PopUpDialog$2.onClick(MainActivity.java:61)
07-08 01:03:15.812: E/AndroidRuntime(28048):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
07-08 01:03:15.812: E/AndroidRuntime(28048):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 01:03:15.812: E/AndroidRuntime(28048):    at android.os.Looper.loop(Looper.java:137)
07-08 01:03:15.812: E/AndroidRuntime(28048):    at android.app.ActivityThread.main(ActivityThread.java:4745)
07-08 01:03:15.812: E/AndroidRuntime(28048):    at java.lang.reflect.Method.invokeNative(Native Method)
07-08 01:03:15.812: E/AndroidRuntime(28048):    at java.lang.reflect.Method.invoke(Method.java:511)
07-08 01:03:15.812: E/AndroidRuntime(28048):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-08 01:03:15.812: E/AndroidRuntime(28048):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-08 01:03:15.812: E/AndroidRuntime(28048):    at dalvik.system.NativeStart.main(Native Method)

Your EditText is null because it is part of popup_signin and you are getting it from loginLayout . 您的EditText为null,因为它是popup_signin一部分,并且您可以从loginLayout中获取它。

So In your case you should Inflate your XML layout into a View object , then find the editText in the view - then you can pass your view to the builder.ie Change 因此,在您的情况下,您应该将XML布局添加到View object ,然后在view找到editText然后可以将视图传递给editText

 LayoutInflater inflater = getActivity().getLayoutInflater();
 builder.setView(inflater.inflate(R.layout.popup_signin, null))

to

LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = (View) inflater.inflate(R.layout.popup_signin, null)
builder.setView(view)

and in Onclick 然后在Onclick中

String username = ((EditText)loginLayout.findViewById(R.id.username)).getText().toString();

to

String username = ((EditText) view.findViewById(R.id.username)).getText().toString();

ie
Rewrite your code as 将您的代码重写为

public Dialog onCreateDialog(Bundle savedInstanceState){
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        //get the layout inflater
        final LinearLayout loginLayout = (LinearLayout) findViewById(R.id.popup_signin);

        LayoutInflater inflater = getActivity().getLayoutInflater();
        final View view = (View) inflater.inflate(R.layout.popup_signin, null)
        builder.setView(view))

                .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int d){
                        //sign in the user...
                        String username = ((EditText) view.findViewById(R.id.username)).getText().toString();
                        //String password = ((EditText) findViewById(R.id.password)).getText().toString();
                        Toast.makeText(getApplicationContext(), "Hello World "+username, Toast.LENGTH_LONG).show();
                       // new PostLogin(username,password).execute();

                    }

You're trying to find your EditText in loginLayout, that is null. 您试图在loginLayout中找到您的EditText,这是空的。 Inflate your layout then assign it to loginLayout. 放大您的布局,然后将其分配给loginLayout。 Like: 喜欢:

LayoutInflater inflater = getActivity().getLayoutInflater();
View loginLayout = inflater.inflate(R.layout.popup_signin, null);

builder.setView(loginLayout);
...

I used a combination of the first answer and the second answer and it worked 我使用了第一个答案和第二个答案的组合,并且有效

LayoutInflater inflater = getActivity().getLayoutInflater();
View loginLayout = inflater.inflate(R.layout.popup_signin, null);

eFullName = (AppCompatEditText) loginLayout.findViewById(R.id.eName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM