alimaqsood
alimaqsood

Reputation: 51

Cannot get a reference to EditText using 'findViewById()' after 'setContentView()'

In an activity where the user is supposed to enter his credentials, in the 'onCreate(...)' of activity, after setContentView(layout) I am doing this:

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

Where nameView is an EditText. When I try to get the text from this variable on a button click, my application crashes. The reason behind it is that nameView holds a null object reference. I can't really understand the reason behind because the function 'findViewById(...)' returns a valid reference to every other View object like Button, Spinner, Image but returns null when called for EditText. I have rechecked the ids multiple times and cross checked almost every little dependency, googled a lot but can't really find the solution, or the cause of the problem.

Bottom line is that the function findViewById is returning null to an object which clearly exists in the layout file with the same id used for the search.

Any help regarding this is appreciated.

This is my Activity

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

These are my layout files

<?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>

The above file includes the code given below

<?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>

Upvotes: 0

Views: 750

Answers (2)

Eugen Pechanec
Eugen Pechanec

Reputation: 38243

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

view is the button clicked. Something tells me that the text box you're looking for is not inside the button.

You created your view variables so you don't have to findViewById all the time. Use them:

EditText t = nameView;

Upvotes: 0

Shadab Ansari
Shadab Ansari

Reputation: 7070

You are initializing the profileNameTextBox at multiple places . At one place you are using -

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

to initialize profileNameTextBox.

Remove the initialization from saveContact(), onBackPressed() and from anywhere else.You should initialize it only once in loadViews(). That's it. And when you want to access it you can use nameView.

Upvotes: 1

Related Questions