通过控制器访问 HashMap 时出现问题

发布于 2024-12-25 10:57:47 字数 1106 浏览 1 评论 0原文

我在通过 Controller 类从 HashMap 返回值时遇到一些麻烦,

  public class Controller {

private User user;
private FileRead fileRead;
private ICS1Activity start;



public Controller(){

user = new User();
fileRead = new FileRead();
start = new ICS1Activity();

}
public User getUser() {
return user;    
}
public FileRead getFileRead() {
return fileRead;
}
public ICS1Activity getStart() {
    return start;       
}
}




public class FileRead {


Controller controller;
private HashMap <String,String> userList = new HashMap <String,String>();


public HashMap<String, String> getUserList() {

 userList.put("fredkr", "value");

    return userList;
}



public void setUserList(HashMap<String, String> userList) {
    this.userList = userList;
}

然后在第三个类(ICS1Activity)中我想将 TextView 字段的文本设置为 HashMap 的值,我尝试使用

testTxt 执行此操作。 setText(controller.getFileRead().getUserList().get("fredkr"));

但这不起作用,程序崩溃了,我很确定这与我访问 HashMap 的方式有关,如果有人可以帮助我,我将非常感激(我知道它一团糟......)

问候,

设法解决了我在这里遇到的问题,但我又遇到了类似的问题。我想我需要阅读控制器和 MVC 结构。有人有什么好的阅读技巧、视频或其他东西吗?

Im having some troubles with returning values from a HashMap trough a Controller class,

  public class Controller {

private User user;
private FileRead fileRead;
private ICS1Activity start;



public Controller(){

user = new User();
fileRead = new FileRead();
start = new ICS1Activity();

}
public User getUser() {
return user;    
}
public FileRead getFileRead() {
return fileRead;
}
public ICS1Activity getStart() {
    return start;       
}
}




public class FileRead {


Controller controller;
private HashMap <String,String> userList = new HashMap <String,String>();


public HashMap<String, String> getUserList() {

 userList.put("fredkr", "value");

    return userList;
}



public void setUserList(HashMap<String, String> userList) {
    this.userList = userList;
}

then in a third class (ICS1Activity) i want to set the the text of a TextView field to the value of the HashMap, I've tried doing this with

testTxt.setText(controller.getFileRead().getUserList().get("fredkr"));

But this does not work and the program crashes, I'm quite sure it has to do with how i access the HashMap, if someone can help me it would me much appreciated (i know its a mess...)

Regards,

Manage to solve the problem i had here, but I'm stuck again with similar issue. think i need to read up on controller and MVC structure. Anyone got any good reading tips, videos or something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪若未夕 2025-01-01 10:57:47

日志猫告诉您有些内容为空,所以也许您忘记初始化控制器?
看一下这个文件:fredkr.ics1.ICS1Activity.onCreate(ICS1Activity.java:46)

The Log cat tells you that something is null, so maybe you forgot to initialize the controller?
Have a look at this file: fredkr.ics1.ICS1Activity.onCreate(ICS1Activity.java:46)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文