2014-05-13 2 views
0

최근에 일식 ADT를 업데이트했습니다. 이제 새로운 액티비티를 시작할 때, 더 많은 메소드가 correspondig 클래스에 구현되어 있고 연결해야하는지 잘 모르겠습니다. 해당 뷰에 내 변수는 .. 변경 어쨌든, 나는 이런 식으로 일을 해요 :안드로이드 : EditText getText() 메소드를 호출 할 때 NullPointerException이 발생했습니다

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()); 
} 

을하지만 ...이 글고의 내가 얻을 널 포인터 예외 내부의 텍스트를 얻을 때

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" /> 

누군가가 나를 도울 수 있습니까? 감사합니다. 당신의 활동에

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

을하지만,이 요소는 조각에 있습니다

+0

show activity_login.xml – Suvitruf

+0

어디에서 doLogin을 호출합니까? – user2450263

+0

게시글을 @Suvitruf – yat0

답변

3

editTextNumeroCartaoeditTextPasswordfragment_login.xml에 널 (null) 그러나 당신이 setContentView(R.layout.activity_login);를 사용하는 이유가있다 생각합니다.

언급 한 EditText을 XML에서 찾을 수 없기 때문에 이것이 사용자에게 NullPointerException을 제공하는 이유입니다.

중 하나를 변경 setContentView(R.layout.activity_login);로 이동합니다 EditText 코드

또는

setContentView(R.layout.fragment_login);Fragment

+0

당신이 맞아 ..이 새로운 변화로 나는이 2 가지 레이아웃으로 끝나고 정말로 변화시킬 줄을 알지 못했다. 고마워! :) – yat0

1

흠, 당신은 시도. 나는 그들이

2

당신은 당신의 EDITTEXT 모두 조각 레이아웃에 선언하지만 코드에서 당신이 activity_man.xml를 통해 액세스했습니다 .

조각화 레이아웃으로 변경하거나 편집 문구 관련 코드를 조각으로 이동하십시오.

관련 문제