'setContentView()'之后无法使用'findViewById()'来获取对EditText的引用

时间:2016-04-28 19:38:57

标签: java android

在用户应该输入其凭据的活动中,在活动的'onCreate(...)'中,在setContentView(布局)之后,我这样做:

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

其中nameView是EditText。当我尝试通过单击按钮从此变量中获取文本时,我的应用程序崩溃了。其背后的原因是nameView包含一个空对象引用。我无法真正理解背后的原因,因为函数'findViewById(...)'返回对Button,Spinner,Image等每个其他View对象的有效引用,但在调用EditText时返回null。我已多次重新检查id并交叉检查几乎所有的小依赖,google了很多但无法真正找到解决方案,或问题的原因。

底线是函数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>

2 个答案:

答案 0 :(得分:1)

您正在多个地方初始化profileNameTextBox。 在您正在使用的一个地方 -

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

初始化profileNameTextBox

saveContact()onBackPressed()和其他任何地方删除初始化。您应该只在loadViews()初始化一次。而已。当您想要访问它时,您可以使用nameView

答案 1 :(得分:0)

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

view是点击的按钮。有些东西告诉我你要找的文本框不在按钮内。

您创建了视图变量,因此您不必始终findViewById。使用它们:

EditText t = nameView;