将edittext的价值转化为对象

时间:2015-04-29 22:38:04

标签: java android android-edittext

嗨,我是Android编程新手,遇到了令人沮丧的bug。我正在尝试从edittext获取值并将其放入对象中。我的代码会将值打印到Log.v中,但是当我尝试在我的setter方法中使用mEdit.getText()。toString()时会抛出NullPointerException。这是代码:

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

                }
            }
        );


    }

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>

和我正在尝试使用的课程:

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

}

此行发生错误:

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

这是日志:

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)

感谢您的帮助!

0 个答案:

没有答案