简体   繁体   中英

Getting value of edittext into object

Hi I'm new to android programming and am running into a frustrating bug. I'm trying to get the value from an edittext and put it into an object. My code will print the value into Log.v but throws a NullPointerException when I try to use mEdit.getText().toString() in my setter method. Here's the code:

 Button mButton;
    EditText mEdit;
    WorkoutTop workoutTop;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_workout);

        mButton = (Button) findViewById(R.id.addButton);
        mEdit = (EditText) findViewById(R.id.addEditText);

        mButton.setOnClickListener(
            new View.OnClickListener()
            {
                public void onClick(View view)
                {
                    Log.v("This works: ", mEdit.getText().toString());
                    workoutTop.setName(mEdit.getText().toString());
                    Log.v("Never gets here:", workoutTop.getName());
                    }

                }
            }
        );


    }

The 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="eric.hork.AddWorkout">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/addEditText"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"
        android:id="@+id/addButton"
        android:layout_below="@+id/addEditText"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

and the Class I'm trying to use:

package eric.hork.Workout;

import java.util.ArrayList;

/**
 * Created by eric on 4/29/15.
 */
public class WorkoutTop {
    private String name;
    private ArrayList<WorkoutDay> workoutDays;

    public String getName(){
        return name;
    }

    public void setName(String workoutName){
        name = workoutName;
    }

    public ArrayList<WorkoutDay> getWorkoutDays(){
        return workoutDays;
    }

    public Boolean addWorkoutDay(WorkoutDay workoutDay){
       return workoutDays.add(workoutDay);
    }

}

The error occurs in this line:

                        workoutTop.setName(mEdit.getText().toString());

Here's the log:

04-29 16:22:37.131  20937-20937/eric.hork E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: eric.hork, PID: 20937
    java.lang.NullPointerException: Attempt to invoke virtual method 'void eric.hork.Workout.WorkoutTop.setName(java.lang.String)' on a null object reference
            at eric.hork.AddWorkout$1.onClick(AddWorkout.java:35)
            at android.view.View.performClick(View.java:5197)
            at android.view.View$PerformClick.run(View.java:20926)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5942)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

Thanks for any help!

您必须初始化workoutTop变量。

workoutTop = new WorkoutTop();

Within your onCreate Method you must initialize workoutTop.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_workout);
    workoutTop = new WorkoutTop(); //add this

    mButton = (Button) findViewById(R.id.addButton);
    mEdit = (EditText) findViewById(R.id.addEditText);

    mButton.setOnClickListener(
        new View.OnClickListener()
        {
            public void onClick(View view)
            {
                Log.v("This works: ", mEdit.getText().toString());
                workoutTop.setName(mEdit.getText().toString());
                Log.v("Never gets here:", workoutTop.getName());
                }

            }
        }
    );


}

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