简体   繁体   中英

editText.getText() returns null

I have tried searching for answers but i haven't found the fix for my problem yet. I have 2 activities -apple and Bacon. I am trying to learn intents and data passing between activities. The apple activity is supposed to pass the value entered in the EditText field on a button click to the bacon activity which just displays the value. I am getting the following exception on the stack

    Caused by: java.lang.NullPointerException: Attempt to invoke 
virtual method 'android.text.Editable android.widget.EditText.getText()' 
on a null object reference

The code snippets are as follows:-

apples.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Apple"
android:background="#009900">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Apples"
    android:id="@+id/applesText"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Go to Bacon"
    android:id="@+id/applesButton"
    android:layout_below="@+id/applesText"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="104dp"
    android:onClick="onClickApples"/>

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:id="@+id/personName"
    android:layout_below="@+id/applesText"
    android:layout_centerHorizontal="true" />

Apples.java

    package com.example.karan.intentexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.content.Intent;
import android.widget.EditText;

public class Apple extends AppCompatActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_apple, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void onClickApples(View view){

        final EditText applesInput = (EditText)view.findViewById(R.id.personName);
        String userMessage = applesInput.getText().toString();
        Intent i = new Intent(this, Bacon.class);
        //putExtra takes data in the form of a key-value pair
        i.putExtra("appleMessage", userMessage);
        startActivity(i);
        }
    }

Bacon.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.karan.intentexample.Bacon"
android:background="#72231F">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Bacon"
    android:id="@+id/baconText"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="39dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/baconButton"
    android:layout_below="@+id/baconText"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="99dp" />

Bacon.java

    package com.example.karan.intentexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class Bacon extends AppCompatActivity {

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

        Bundle applesData;
        applesData = getIntent().getExtras();

        //if there is no data in the intent
        if(applesData==null)
            return;

        //get value by using the key in the getString method
        String appleMessage = applesData.getString("appleMessage");
        final TextView baconText = (TextView)findViewById(R.id.baconText);
        baconText.setText(appleMessage);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_bacon, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Here:

final EditText applesInput = (EditText)view.findViewById(R.id.personName);

view.findViewById causing issue.change it to:

final EditText applesInput = (EditText)Apple.this.findViewById(R.id.personName);

Because EditText with personName id is in Activity layout instead of inside Button layout(view parameter of onClickApples)

public class Apple extends AppCompatActivity {

    private EditText applesInput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apple);
        applesInput = (EditText)findViewByID(R.id.personName);
    }
    public void onClickApples(View view){
        String userMessage = applesInput.getText().toString();
        Intent i = new Intent(this, Bacon.class);
        //putExtra takes data in the form of a key-value pair
        i.putExtra("appleMessage", userMessage);
        startActivity(i);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM