【问题标题】:Android getText from EditText return NULL来自 EditText 的 Android getText 返回 NULL
【发布时间】:2014-07-23 07:05:18
【问题描述】:

在我在 Android 中的第一个程序的这个简单应用程序中,我想从用户那里获得 usernamepassword。但是在单击返回 NULL 的按钮后,这是不正确的,usernamepassword 字段具有字符串,而不是 NULL。 此代码中的TextusernameTextpasswordNULL,无法从R.id.username AND R.id.password 获取字符串

我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >

        <TextView
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:text="@string/EnterUsername"
                />
        <EditText
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:id="@+id/username"
                />
    </LinearLayout>
    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >

        <TextView
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:text="@string/EnterPassword"
                />
        <EditText
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:id="@+id/password"
                />
    </LinearLayout>
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            />
    <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/submitButton"
            android:text="@string/SubmitButton"
            android:gravity="center"
            android:background="@drawable/purple"/>
</LinearLayout>

我的代码:

package com.example.AndroidMultiPage;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final EditText username = (EditText) findViewById(R.id.username);
        final EditText password = (EditText) findViewById(R.id.password);

        Button   submit   = (Button)   findViewById(R.id.submitButton);

        final String Textusername = username.getText().toString();
        final String Textpassword = password.getText().toString();

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(
                         MyActivity.this, Textusername, 
                         Toast.LENGTH_SHORT).show();

                //TOAST SHOW NULL
            }
        });
    }
}

【问题讨论】:

    标签: android android-layout android-activity


    【解决方案1】:

    你应该搬家

     final String Textusername = username.getText().toString();
     final String Textpassword = password.getText().toString();
    

    在提交ButtononClick(..)onClick(..) 内,您应该检查String 的值是否为NULL。正如@Raghunandan 所说...

    【讨论】:

      【解决方案2】:

      请移步

      final String Textusername = username.getText().toString(); final String Textpassword = password.getText().toString();

      onClick().

      【讨论】:

      • 谢谢。之后我收到此错误Unfortundately SimpleIntent has Stoped
      • 那么请贴一下logcat。
      【解决方案3】:

      移动这个

      String Textusername = username.getText().toString();
      String Textpassword = password.getText().toString();
      

      onClick 内。

      也检查一下

      if(!TextUtils.isEmpty(Textusername))
      {
                 // display Toast 
      }
      

      【讨论】:

        【解决方案4】:
        submit.setOnClickListener(new OnClickListener() {
        
        
             @Override
                        public void onClick(View view) {
        
                           String Textusername = username.getText().toString();
        
                            Toast.makeText(MyActivity.this, Textusername, Toast.LENGTH_SHORT).show();
        
                            //TOAST SHOW NULL
                        }
                    });
        

        【讨论】:

          【解决方案5】:

          试试下面的代码:

          submit.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  final String Textusername = username.getText().toString();
                  final String Textpassword = password.getText().toString();
          
                  Toast.makeText(MyActivity.this, Textusername, Toast.LENGTH_SHORT).show();    
              }
          });
          

          【讨论】:

            【解决方案6】:
            Use this:
             submit.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
            String text_user=username.getText().toString();
                            Toast.makeText(
                                     MyActivity.this, text_user, 
                                     Toast.LENGTH_SHORT).show();
            
                                        }
                    });
            

            【讨论】:

              猜你喜欢
              • 2011-11-04
              • 1970-01-01
              • 2013-05-28
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-01-30
              相关资源
              最近更新 更多