2016-04-28 61 views
0

在用戶應該輸入其憑據的活動中,在活動的'onCreate(...)'中,在setContentView佈局)我這樣做:無法在'setContentView()'後使用'findViewById()'獲取對EditText的引用

nameView = (EditText) findViewById(R.id.profileNameTextBox); 

其中nameView是一個EditText。當我嘗試從單擊按鈕上的該變量中獲取文本時,我的應用程序崩潰。它背後的原因是nameView保存一個空對象引用。我無法真正理解背後的原因,因爲函數'findViewById(...)'返回每個其他View對象(如Button,Spinner,Image)的有效引用,但在調用EditText時返回null。我已經多次重新檢查了ID並交叉檢查了幾乎所有的小型依賴項,搜索了很多,但無法真正找到解決方案或問題的原因。

底線是findViewById函數將null返回給佈局文件中清楚存在的對象,該對象具有用於搜索的相同ID。

任何有關這方面的幫助表示讚賞。

這是我的活動

package theappman.speedcontacts; 

import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.Spinner; 
import android.widget.TextView; 
import android.widget.Toast; 

public class AddContact extends AppCompatActivity { 

    Contact currentContact; 

    private EditText nameView; 
    private EditText numberView; 
    private ImageView imageView; 
    private Spinner dayView; 
    private Spinner monthView; 
    private Spinner yearView; 
    private Button saveView; 

    private void loadViews(){ 

     numberView = (EditText) findViewById(R.id.profileNumberTextBox); 
     imageView = (ImageView) findViewById(R.id.profileImage); 
     dayView = (Spinner) findViewById(R.id.dateSpinner); 
     monthView = (Spinner) findViewById(R.id.monthSpinner); 
     yearView = (Spinner) findViewById(R.id.yearSpinner); 
     saveView = (Button) findViewById(R.id.saveButton); 
     nameView = (EditText) findViewById(R.id.profileNameTextBox); 
    } 

    private void populateSpinners(){ 
     String[] days = new String[32]; 
     String[] months = new String[13]; 
     String[] years = new String[71]; 

     days[0] = "Day"; 
     months[0] = "Month"; 
     years[0] = "Year"; 

     for (int i=1; i<32; i++){ 
      days[i] = String.valueOf(i+1); 
     } 

     for (int i=1,j=69; i<71; i++,j--){ 
      years[i] = String.valueOf(1947+j); 
     } 

     months[1] = "January"; 
     months[2] = "February"; 
     months[3] = "March"; 
     months[4] = "April"; 
     months[5] = "May"; 
     months[6] = "June"; 
     months[7] = "July"; 
     months[8] = "August"; 
     months[9] = "September"; 
     months[10] = "October"; 
     months[11] = "November"; 
     months[12] = "December"; 

     ArrayAdapter<String> dayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, days); 
     dayView.setAdapter(dayAdapter); 
     ArrayAdapter<String> monthAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, months); 
     monthView.setAdapter(monthAdapter); 
     ArrayAdapter<String> yearAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, years); 
     yearView.setAdapter(yearAdapter); 


    } 


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

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     currentContact = new Contact(); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
       //  .setAction("Action", null).show(); 
      } 
     }); 
     loadViews(); 
     populateSpinners(); 

     saveView.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       if (nameView != null) { 
        String nnn = nameView.getText().toString(); 
        Toast.makeText(AddContact.this, nnn,Toast.LENGTH_LONG).show(); 
       } 
       Toast.makeText(AddContact.this, "not.",Toast.LENGTH_LONG).show(); 

      } 
     }); 
    } 

    public void saveContact(View view){ 
     EditText t = (EditText) view.findViewById(R.id.profileNameTextBox); 
     String NAME = t.getText().toString(); 
     //String NUMBER = view.findViewById(R.id.profileNumber).toString(); 
     Toast.makeText(AddContact.this, "Ok.",Toast.LENGTH_LONG).show(); 
     /*String DATE = day.getSelectedItem().toString() + "-" + month.getSelectedItem().toString() + "-" + year.getSelectedItem().toString(); 
     if (NAME.equals("") || NUMBER.equals("")){//|| day.getSelectedItem().toString().equals("Day") || month.getSelectedItem().toString().equals("Month") || year.getSelectedItem().toString().equals("Year")){ 
      Toast.makeText(this, "Please fill all fields.",Toast.LENGTH_LONG).show(); 
      return; 
     }*/ 
     //currentContact.setName(NAME); 
     //currentContact.setNumber(NUMBER); 
     //currentContact.setDateOfBirth(DATE); 
     //save currentContact in database 
     /*Intent intent = getIntent(); 
     intent.putExtra("key", "Contact Saved"); 
     setResult(RESULT_OK, intent); 
     finish();*/ 
    } 


    @Override 
    public void onBackPressed() { 
     nameView = (EditText) findViewById(R.id.profileNameTextBox); 
     Intent intent = getIntent(); 
     intent.putExtra("key", nameView.getText().toString()); 
     setResult(RESULT_OK, intent); 
     finish(); 
     super.onBackPressed(); 
    } 
} 

這是我的佈局文件

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="theappman.speedcontacts.AddContact"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_add_contact" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 

上述文件包括以下

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="theappman.speedcontacts.AddContact" 
    tools:showIn="@layout/activity_add_contact"> 

    <ImageView 
     android:id="@+id/profileImage" 
     android:src="@drawable/ic_person_black_48dp" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" /> 
    <Button 
     android:id="@+id/selectImageButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:text="Select Picture" 
     /> 

    <EditText 
     android:name="@+id/profileNameTextBox" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Enter Full Name"/> 

    <EditText 
     android:name="@+id/profileNumberTextBox" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="phone" 
     android:hint="Enter Phone Number"/> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_gravity="center" 
     android:orientation="horizontal" 
     android:weightSum="3" 
     > 

     <Spinner 
      android:layout_weight="1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:spinnerMode="dropdown" 
      android:id="@+id/dateSpinner" 
      /> 

     <Spinner 
      android:layout_weight="1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:spinnerMode="dropdown" 
      android:id="@+id/monthSpinner"/> 

     <Spinner 
      android:layout_weight="1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:spinnerMode="dropdown" 
      android:id="@+id/yearSpinner"/> 

    </LinearLayout> 

    <Button 
     android:id="@+id/saveButton" 
     android:layout_width="96dp" 
     android:layout_height="wrap_content" 
     android:text="Save" 
     android:layout_gravity="right" 

     /> 


</LinearLayout> 

回答

1

您初始化profileNameTextBox在多個地方提供的代碼。 在一個地方使用的是 -

EditText t = (EditText) view.findViewById(R.id.profileNameTextBox); 

初始化profileNameTextBox

saveContact()onBackPressed()和其他任何地方刪除初始化。您應該在loadViews()中將其初始化一次。而已。而當你想訪問它時,你可以使用nameView

+0

這只是爲了檢查。即使我初始化一次也沒有工作。 – alimaqsood

+0

「你沒有工作」是什麼意思? –

+0

我又得到了null。即使我初始化一次。 – alimaqsood

0
public void saveContact(View view){ 
    EditText t = (EditText) view.findViewById(R.id.profileNameTextBox); 

view是點擊的按鈕。東西告訴我,你正在尋找的文本框不在按鈕內。

您創建了您的視圖變量,因此您不必始終使用findViewById。使用它們:

EditText t = nameView; 
相關問題