editText.getText()返回null

时间:2015-09-28 16:56:17

标签: android android-intent

我试过寻找答案,但我还没找到解决问题的方法。我有2个活动 - 苹果和培根。我正在尝试学习活动之间传递的意图和数据。 apple活动应该通过单击按钮上的EditText字段中输入的值传递给只显示值的培根活动。 我在堆栈上得到以下异常

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

代码片段如下: -

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

2 个答案:

答案 0 :(得分:1)

这里:

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

view.findViewById导致问题。将其更改为:

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

因为带有EditText id的personName在Activity布局中而不是在Button布局内(onClickApples的view参数)

答案 1 :(得分:1)

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