我遇到以下错误:尝试在空对象引用上调用虚拟方法'boolean java.util.ArrayList.add(java.lang.Object)'''' 我的保存按钮在设置活动中,该活动调用输入以转到将在历史记录活动中显示的listView。
Settings.java:
public class settingsActivity extends MainActivity {
Button saveButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
goaldAte = findViewById(R.id.goaldinput);
saveButton=findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String goalD = goaldAte.getText().toString();
adapter = new ArrayAdapter<String>(settingsActivity.this,R.layout.activity_history,arrayList);
arrayList.add(goalD);
adapter.notifyDataSetChanged();
}
});
}
}
History.java
public class historyActivity extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
listView= findViewById(R.id.goaldlistView);
String addArray[] = {"3141992"};
arrayList=new ArrayList<String>(Arrays.asList(addArray));
listView.setAdapter(adapter);
}
}
答案 0 :(得分:1)
在您的
setContentView(R.layout.activity_history);
您正在告诉该活动指向此XML布局。但是,您的按钮位于其他xml。所以这就是你的错误
答案 1 :(得分:0)
可能是您为其分配 saveButton 的按钮,在
的布局中似乎不存在R.layout.activity_history
确保在相应的XML文件中存在一个ID为 saveButton 的按钮。