簡體   English   中英

Android:調用EditText getText()方法時出現NullPointerException

[英]Android: NullPointerException when calling EditText getText() method

我最近更新了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" />

有人可以幫我嗎? 謝謝 !

editTextNumeroCartaoeditTextPasswordfragment_login.xml但是您正在使用setContentView(R.layout.activity_login);

這就是它給您NullPointerException原因,因為在XML中找不到所提到的EditText

要么更改setContentView(R.layout.activity_login); 設置setContentView(R.layout.fragment_login);

要么

EditText代碼移到您的Fragment

您已經在片段布局中聲明了兩個editText,但是在您的代碼中將通過activity_man.xml進行訪問。

將其更改為Fragement布局,或將與edittext相關的代碼移動到Fragment。

嗯,您正在嘗試:

editTextNumeroCartao = (EditText) findViewById(R.id.editTextNumeroCartao);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);

在您的活動中,但此元素在Fragment中。 我認為這是因為它們為空

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM