Android:调用EditText getText()方法时出现NullPointerException

时间:2014-05-13 09:02:18

标签: java android xml

我最近更新了我的eclipse ADT,现在当我开始新的活动时,我已经在对应类中实现了更多的方法,并且我不确定我是否应该将我的变量连接到相应的视图已经改变..无论如何,我这样做:

public class LoginActivity extends ActionBarActivity {

EditText editTextNumeroCartao,
         editTextPassword;

InternetConnectivityManager icm;

ProgressDialog pDialog;
UserFunctions userFunction;
SessionManager session;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    // conecta as variáveis ás respectivas views
    editTextNumeroCartao = (EditText) findViewById(R.id.editTextNumeroCartao);
    editTextPassword = (EditText) findViewById(R.id.editTextPassword);

    icm = new InternetConnectivityManager(getApplicationContext());
}

但是当我试图在这个EditText中获取文本时,我得到Null Pointer Exception ...

public void doLogin(View view) {

    if(icm.isOnline()) {

        String numeroCartao = this.editTextNumeroCartao.getText().toString();
        String password = this.editTextPassword.getText().toString();

        if(numeroCartao == null || numeroCartao.trim().equals("") || password == null || password.trim().equals("")) {
            Toast.makeText(getApplicationContext(), "Insere todos os dados", Toast.LENGTH_LONG).show();
        } else {
            new LoginAsyncTask().execute(numeroCartao, password);
        }

    } else {
        Toast.makeText(getApplicationContext(), "Não estás ligado à Internet", Toast.LENGTH_LONG).show();
    }

    return;
}

我的activity_login xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="me.pap.bruno.esdahconnect.LoginActivity"
tools:ignore="MergeRootFrame" />

我的fragment_login xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="me.pap.bruno.esdahconnect.LoginActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Número do Cartão"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editTextNumeroCartao"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:text="Password"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editTextPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPassword" />

<Button
    android:id="@+id/buttonLogin"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:text="Entrar" 
    android:onClick="doLogin" />

有人能帮助我吗? 谢谢!

3 个答案:

答案 0 :(得分:3)

editTextNumeroCartaoeditTextPassword位于fragment_login.xml,但您使用的是setContentView(R.layout.activity_login);

这就是它给你NullPointerException的原因,因为在XML中找不到提到的EditText

setContentView(R.layout.activity_login);更改为setContentView(R.layout.fragment_login);

EditText代码移至Fragment

答案 1 :(得分:2)

您的两个editText都是在片段布局中声明的,但在您的代码中,您通过activity_man.xml进行访问。

将此更改为Fragement布局或将与edittext相关的代码移动到片段。

答案 2 :(得分:1)

嗯,你在尝试:

editTextNumeroCartao = (EditText) findViewById(R.id.editTextNumeroCartao);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
在您的Activity中

,但这些元素在Fragment中。我认为它们是空的原因