好的,这是我的第一篇文章,所以请耐心等待。我搜索过并试过很多东西。我认为另一组眼睛可能会看到我没有的东西。当我调用getText()函数时,我得到了NPE。我已经包含了我的代码的缩小版本,但同样的错误正在发生。我甚至看到我有正确的观点任何帮助将不胜感激。
MainActivity.java
const string MathCourseName = "Math";
var Student1 = new Student("Alice");
Student1.Infos.Add(new Info(MathCourseName, 4));
var Student2 = new Student("Bob");
Student2.Infos.Add(new Info(MathCourseName, 2));
var Student3 = new Student("Cesar");
Student3.Infos.Add(new Info("English", 3));
var repository = new StudentRepository();
repository.Add(Student1);
repository.Add(Student2);
repository.Add(Student3);
foreach(var kvp in repository.GetCoursesByStudentName(MathCourseName)) {
Console.WriteLine(kvp.Key + ": " + kvp.Value.Course + " - " + kvp.Value.Grade);
}
var bobsMathGrade = repository.GetGrade("Bob", MathCourseName);
Console.WriteLine("Bobs math grade: " + bobsMathGrade);
task_add_operations.xml
public class MainActivity extends AppCompatActivity {
private ArrayList<Task> taskList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater inflater2 = (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View customView2 = inflater2.inflate(R.layout.task_add_operations,null);
final PopupWindow mPopupWindow2 = new PopupWindow(customView2,
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT, true);
Button closeButton2 = (Button)customView2.findViewById(R.id.closeButton2);
closeButton2.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
mPopupWindow2.dismiss();
}
});
Button addButton = (Button)customView2.findViewById(R.id.addButton);
addButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View customView2) {
EditText text = customView2.findViewById(R.id.editText);
//This is where my java.lang.NullPointerException pops
String title = text.getText().toString();
taskList = Task.addTask(taskList, title);
mPopupWindow2.dismiss();
}
});
mPopupWindow2.showAtLocation(findViewById(R.id.activityMain),
CENTER,0, 0);
}
});
}
答案 0 :(得分:2)
您需要绑定EditText
外addButton.setOnClickListener
EditText text =(EditText) customView2.findViewById(R.id.editText);
或尝试更改view
public void onClick(View View2)
方法的名称,如下面的代码
Button addButton = (Button)customView2.findViewById(R.id.addButton);
addButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View View2) {
EditText text = (EditText)customView2.findViewById(R.id.editText);
//This is where my java.lang.NullPointerException pops
String title = text.getText().toString();
taskList = Task.addTask(taskList, title);
mPopupWindow2.dismiss();
}
});
答案 1 :(得分:0)
从查看其他Stack Overflow帖子:
Null Pointer Exception - Getting EditText Value
您可能希望尝试在EditText
OnCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addButton = (Button)customView2.findViewById(R.id.addButton);
EditText text = customView2.findViewById(R.id.editText);
addButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View customView2) {
//This is where my java.lang.NullPointerException pops
String title = text.getText().toString();
taskList = Task.addTask(taskList, title);
mPopupWindow2.dismiss();
}
});