gpt4 book ai didi

java - 将 edittext 的值获取到对象中

转载 作者:行者123 更新时间:2023-12-01 11:36:18 24 4
gpt4 key购买 nike

嗨,我是 Android 编程新手,遇到了一个令人沮丧的错误。我正在尝试从编辑文本中获取值并将其放入对象中。我的代码会将值打印到 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)

感谢您的帮助!

最佳答案

您必须初始化workoutTop变量。

workoutTop = new WorkoutTop();

关于java - 将 edittext 的值获取到对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29955808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com